【问题标题】:python requests with proxies带有代理的python请求
【发布时间】:2019-11-23 14:54:04
【问题描述】:

在我的脚本中,我试图向 q 代理服务器发出请求。 我只是这样做:

import requests

response = requests.get('https://websiteiwhantget', proxies={"http": '176.36.111.9:56323', "https": '176.36.111.9:56323'})

我从https://free-proxy-list.net/ 获得的代理 IP 地址,但是当我运行 sript 时,我在 get 调用中输入的每个网站都有:

引发 ProxyError(e, request=request) requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.moma.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', RemoteDisconnected('Remote end closed connection)没有回应',)))

如果我删除 requests.get 中的代理指令,一切都已完成。 为什么使用代理我的脚本不起作用?是免费代理列表中列出的代理错误还是我必须更改我的 python 调用? 我使用 python 3.6

提前非常感谢 上午

【问题讨论】:

  • 如果您不介意付费代理解决方案,您可以尝试使用gimmeproxy.com,它将验证其数据库中的所有代理。

标签: python-3.x web-scraping proxy


【解决方案1】:

阅读:https://www.scrapehero.com/how-to-rotate-proxies-and-ip-addresses-using-python-3/

试试这个:

    import requests
    import random
    from lxml.html import fromstring

    url = 'https://free-proxy-list.net/anonymous-proxy.html'
    response = requests.get(url)
    parser = fromstring(response.text)
    proxies = []
    for i in parser.xpath('//tbody/tr')[:20]:
        if i.xpath('.//td[7][contains(text(),"yes")]'):
            proxy = ":".join([i.xpath('.//td[1]/text()')[0], i.xpath('.//td[2]/text()')[0]])

        try:
            t = requests.get("https://www.google.com/", proxies={"http": proxy, "https": proxy}, timeout=5)
            if t.status_code == requests.codes.ok:
                proxies.append(proxy)
        except:
            pass

    proxy = proxies[random.randint(0, len(proxies)-1)]

    response = requests.get('https://websiteiwhantget', proxies={"http": proxy, "https": proxy})

【讨论】:

    猜你喜欢
    • 2021-06-01
    • 2022-01-19
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多