我使用以下代码发送请求,如果它们在文件中,则使用代理,但是,脚本使用的不是代理,而是我的本地IP:
# open proxy file and get the number of proxies and the proxies as a list
f = open('proxies.txt', 'r')
proxy_str = f.read()
line_count = sum(1 for line in f)
proxy_list = proxy_str.replace('\n', ', ').split(', ')
if line_count > 0:
try:
proxy_index = random.randint(0, len(proxy_list) - 1)
proxies = {'http://': proxy_list[proxy_index],
'https://': proxy_list[proxy_index]}
response1 = requests.get(product_link, headers=headers, proxies=proxies)
except:
print("Proxy error")
pass
else:
response1 = requests.get(product_link, headers=headers, proxies=proxies)发布于 2020-05-20 23:20:57
proxies = {
'http:': proxy_list[proxy_index],
'https:': proxy_list[proxy_index]
}发布于 2020-05-21 20:52:27
下面是文档中的一段代码--它对我来说非常有用。请注意proxies字典的键。
https://requests.readthedocs.io/en/master/user/advanced/#proxies
proxies = {
'http': 'http://10.10.1.10:3128', # your proxies here
'https': 'http://10.10.1.10:1080', # your proxies here
}
requests.get('http://example.org', proxies=proxies)https://stackoverflow.com/questions/61916458
复制相似问题