如果文件中有代理,我可以使用这些代码发送请求,但是,脚本使用的不是代理,而是我的本地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)
下面是文档中的一个片段,它对我来说非常有用。注意
proxies
字典的键https://requests.readthedocs.io/en/master/user/advanced/#proxies
相关问题 更多 >
编程相关推荐