从proxies.txt文件中删除代理

2024-04-25 11:55:57 发布

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

如果文件中有代理,我可以使用这些代码发送请求,但是,脚本使用的不是代理,而是我的本地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)

Tags: andthe代理getindexcountlineopen
2条回答
proxies = {
    'http:': proxy_list[proxy_index],
    'https:': proxy_list[proxy_index]
}

下面是文档中的一个片段,它对我来说非常有用。注意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)

相关问题 更多 >