【Python requests 代理服务问题】

在使用Python requests模块进行代理服务时遇到请求错误,问题关键在于代理URL缺少协议头。错误信息提示代理URL需要以'http://'或'https://'开头。通过在代理IP前添加'http://'和'https://'解决了问题。对于Python 3.7以后的版本,代理格式有所变化,需注意语法更新。
摘要由CSDN通过智能技术生成

Python requests 代理服务问题

背景

学习 Python requests 模块 代理服务时,碰到请求报错,特此记录

问题复现

代码:
import requests

proxy = {
    'http': 'xxx.xxx.xx.xxx:808',
    'https': 'xxx.xxx.xx.xxx:8080'
}  # 设置代理 ip 及对应的端口号

#  对需要爬取的网页发送请求
response = requests.get('http://www.xxxxxx.com/', proxies=proxy)
print(response.content.decode)
错误信息
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/adapters.py", line 412, in send
    conn = self.get_connection(request.url, proxies)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/adapters.py", line 309, in get_connection
    proxy_manager = self.proxy_manager_for(proxy)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/adapters.py", line 193, in proxy_manager_for
    manager = self.proxy_manager[proxy] = proxy_from_url(
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/urllib3/poolmanager.py", line 536, in proxy_from_url
    return ProxyManager(proxy_url=url, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/urllib3/poolmanager.py", line 480, in __init__
    raise ProxySchemeUnknown(proxy.scheme)
urllib3.exceptions.ProxySchemeUnknown: Proxy URL had no scheme, should start with http:// or https://

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/kahnlau/Documents/coding/python/jinruicoding/zero_foundation_to_learn_python/chapter_14_web_spider/code1406_proxy.py", line 14, in <module>
    response = requests.get('http://www.mingrisoft.com/', proxies=proxy)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/adapters.py", line 414, in send
    raise InvalidURL(e, request=request)
requests.exceptions.InvalidURL: Proxy URL had no scheme, should start with http:// or https://

分析

最后一句是关键「requests.exceptions.InvalidURL: Proxy URL had no scheme, should start with http:// or https://」
翻译过来大概意思就是 代理 URL 需要以 http:// 或者 https://
尝试将 「http://」 和 「https://」 加入到对应的 IP 前,再次执行,成功
经查阅得知,python3.7之后代理格式就变了,所以解决方案:

  1. 降低 Python 版本到 3.7 以下
  2. 使用新的语法编写
proxy = {
    'http': 'http://xxx.xxx.xx.xxx:808',
    'https': 'https://xxx.xxx.xx.xxx:8080'
}  # 设置代理 ip 及对应的端口号
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值