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

2024-03-28 12:45:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我只是试图使用我所有的代理发送请求,但只有一个代理被使用。你知道吗

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\u requests),但我需要使用自定义代理。你知道吗


Tags: httpsimportregisterurl代理dataismy
1条回答
网友
1楼 · 发布于 2024-03-28 12:45:43

您正在创建代理字典,这是错误的。'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)

相关问题 更多 >