无法在python中的request.get()或request.post()上连接到代理错误 [英] Cannot connect to proxy error on requests.get() or requests.post() in python
问题描述
我有两个URL可以从中获取数据.使用我的代码,第一个URL有效,而第二个URL提供 ProxyError
.
I have two URLs to fetch data from. Using my code, the first URL is working, whereas the second URL is giving ProxyError
.
我正在Python 3中使用 requests
库,并尝试在Google和此处搜索问题,但没有成功.
I am using requests
library in Python 3 and tried searching the problem in Google and here, but with no success.
我的代码段是:
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)
url1
正常工作,但是 url2
给出以下错误:
url1
works fine, but url2
gives following error:
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',)))
使用 request.post()
-
请解释一下为什么会发生这种情况,并且两个URL的握手之间有什么区别吗?
Please explain me why this is happening, and is there any difference between the handshaking of both the URLs?
urllib.request.urlopen
工作正常,所以我明确地使用 requests
urllib.request.urlopen
is working fine, so I am explicity looking for answers using requests
推荐答案
在 User-Agent
中使用headers关键字参数时,我能够对 url2
进行有效的响应字符串设置为 Chrome
.
I was able to illicit a valid response for url2
when using headers keyword argument with User-Agent
string set to Chrome
.
r2 = requests.get(url2, proxies=proxies, headers={'User-Agent': 'Chrome'})
要回答您的第一个问题,发生这种情况的可能原因与服务器端设置有关.可能配置为不接受来自未知代理的请求或缺少 User-Agent
标头的请求.
To answer your first question, possible reason for this happening is related to server-side settings. It might be configured not to accept requests originating from unknown agents or requests with a missing User-Agent
header.
这篇关于无法在python中的request.get()或request.post()上连接到代理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!