Python 请求代理错误 'Failed to parse'

标签 python python-requests

我正在尝试使用代理列表来抓取页面。 这个小问题简直让我抓狂。当我像这样直接输入代理时它有效:

proxies = {
            'http': 'http://10.0.1.1:8080',
            'https': 'http://10.0.1.1:8080'
        }

但是当我使用类似的东西时

http_proxy =  'http://'+proxy
https_proxy = 'https://'+proxy



    proxies = {
            'http': http_proxy,
            'https': https_proxy,
        }

requests.packages.urllib3.exceptions.LocationParseError: Failed to parse: 10.0.1.1:8080

我收到这个错误。这完全没有意义。

编辑:我刚刚意识到这可能是因为每个代理之后的换行符我在服务器上托管了 proxylist.txt 所以现在我需要找出如何在每个代理之后摆脱换行我尝试了代理之类的东西。 strip('\n') 但这也不起作用

最佳答案

因为那个问题我快疯了。

尝试这样做:

def chomp(x):
    if x.endswith("\r\n"):
        return x[:-2]
    if x.endswith("\n") or x.endswith("\r"):
        return x[:-1]
    return x

http_proxy =  'http://' + chomp(proxy)
https_proxy = 'https://' + chomp(proxy)

proxies = {
       'http': http_proxy,
       'https': https_proxy,
    }

它帮助解决了我的问题。

关于Python 请求代理错误 'Failed to parse',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43526243/

相关文章:

python - 合并排序中的比较计数

Python - 迭代列表字典

python - 为什么 Python informixdb 包抛出错误!

python - 无法使用 python 从 URL 下载文件

django - 为什么Python请求库无法得到响应?

Python写入文件

python - 将 matplotlib 图形 Canvas 图像插入 QTextDocument

python - 识别并中断异常 : 'OpenSSL.SSL.Error' using python's requests module

python-requests - C# 相当于 requests.post() 函数

python - 在 Google App Engine 本地开发服务器中使用请求