在周期发送请求时如何使用代理列表中的所有代理?

时间:2019-11-02 18:35:28

标签: python proxy python-requests proxies

我只是尝试使用我的所有代理发送请求,但只使用了一个代理。

this is my code:
import requests
import random
import string
import random
from proxy_requests import ProxyRequests
proxies = {'https': 'https://104.148.46.2:3121',
'https': 'https://134.19.254.2:21231',
'https': 'https://45.76.222.196:8118',
'https': 'https://103.87.207.188:59064',
'https': 'https://182.74.40.146:30909',
'https': 'https://5.196.132.115:3128',
'https': 'https://200.142.120.90:56579',
'https': 'https://24.172.82.94:53281',
'https': 'https://213.141.93.60:32125',
'https': 'https://167.71.6.78:3128',
'https': 'https://202.166.205.78:58431',
'https': 'https://37.82.13.84:8080',
'https': 'https://132.255.23.157:3128'}

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 
OPR/63.0.3368.71'}

while True:

    with requests.Session() as s:
         register_data = {"POST":"CENSORED"}
        url = 'CENSORED'
        r = s.post(url, json=register_data, proxies = proxies)
        print(r.content)
        print (proxies)

输出为:

b'"CENSORED"'
{'https': 'https://132.255.23.157:3128'}
b'"CENSORED"'
{'https': 'https://132.255.23.157:3128'}
b'"CENSORED"'
{'https': 'https://132.255.23.157:3128'}

我需要这样的输出:

b'"CENSORED"'
{'https': 'https://202.166.205.78:58431'}
b'"CENSORED"'
{'https': 'https://103.87.207.188:59064'}
b'"CENSORED"'
{'https': 'https://132.255.23.157:3128'}

我尝试使用代理请求模块(proxy_requests),但是我需要使用自定义代理。

1 个答案:

答案 0 :(得分:0)

您正在创建代理字典,这是错误的。 'https'是一个密钥,只有一个名为https的密钥。您最终将值'https://132.255.23.157:3128'分配给'https',因此,每次打印proxies时,您都会得到{'https': 'https://132.255.23.157:3128'}

您必须为proxies创建字典列表才能获得所需的输出。

import requests
import random
proxies = [{'https': 'https://104.148.46.2:3121'}, {'https': 'https://134.19.254.2:21231'}, {'https': 'https://45.76.222.196:8118'}, {'https': 'https://103.87.207.188:59064'}, {'https': 'https://182.74.40.146:30909'}, {'https': 'https://5.196.132.115:3128'}, {'https': 'https://200.142.120.90:56579'}, {'https': 'https://24.172.82.94:53281'}, {'https': 'https://213.141.93.60:32125'}, {'https': 'https://167.71.6.78:3128'}, {'https': 'https://202.166.205.78:58431'}, {'https': 'https://37.82.13.84:8080'}, {'https': 'https://132.255.23.157:3128'}]

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36OPR/63.0.3368.71'}

while True:
    with requests.Session() as s:
        register_data = {"POST":"CENSORED"}
        url = 'CENSORED'
        r = s.post(url, json=register_data, proxies = random.choice(proxies))
        print(r.content)
        print (proxies)