首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从proxies.txt文件python中抓取代理

从proxies.txt文件python中抓取代理
EN

Stack Overflow用户
提问于 2020-05-20 23:05:36
回答 2查看 377关注 0票数 0

我使用以下代码发送请求,如果它们在文件中,则使用代理,但是,脚本使用的不是代理,而是我的本地IP:

代码语言:javascript
复制
# 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)
EN

回答 2

Stack Overflow用户

发布于 2020-05-20 23:20:57

代码语言:javascript
复制
proxies = {
    'http:': proxy_list[proxy_index],
    'https:': proxy_list[proxy_index]
}
票数 0
EN

Stack Overflow用户

发布于 2020-05-21 20:52:27

下面是文档中的一段代码--它对我来说非常有用。请注意proxies字典的键。

https://requests.readthedocs.io/en/master/user/advanced/#proxies

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61916458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档