the*_*ngh 8 python proxy request http-proxy python-requests
我有两个 URL 可以从中获取数据。使用我的代码,第一个 URL 有效,而第二个 URL 给出ProxyError.
我正在requestsPython 3 中使用库,并尝试在 Google 和此处搜索问题,但没有成功。
我的代码片段是:
import requests
proxies = {
'http': 'http://user:pass@xxx.xxx.xxx.xxx:xxxx',
'https': 'http://user:pass@xxx.xxx.xxx.xxx:xxxx',
}
url1 = 'https://en.oxforddictionaries.com/definition/act'
url2 = 'https://dictionary.cambridge.org/dictionary/english/act'
r1 = requests.get(url1, proxies=proxies)
r2 = requests.get(url2, proxies=proxies)
Run Code Online (Sandbox Code Playgroud)
url1工作正常,但url2出现以下错误:
ProxyError: HTTPSConnectionPool(host='dictionary.cambridge.org', port=443): Max retries exceeded with url: /dictionary/english/act (Caused by ProxyError('Cannot connect to proxy.', RemoteDisconnected('Remote end closed connection without response',)))
Run Code Online (Sandbox Code Playgroud)
使用时也会发生同样的情况request.post()
请解释一下为什么会发生这种情况,以及两个 URL 的握手之间有什么区别吗?
urllib.request.urlopen工作正常,所以我明确使用以下方式寻找答案requests
url2当使用 headers 关键字参数并将User-Agent字符串设置为 时,我能够非法获得有效响应Chrome。
r2 = requests.get(url2, proxies=proxies, headers={'User-Agent': 'Chrome'})
Run Code Online (Sandbox Code Playgroud)
要回答您的第一个问题,发生这种情况的可能原因与服务器端设置有关。它可能被配置为不接受来自未知代理的请求或缺少User-Agent标头的请求。