如何为请求会话对象设置单个代理?
How can I set a single proxy for a requests session object?
我正在使用 Python 请求包发送 http 请求。我想向请求会话对象添加一个代理。例如
session = requests.Session()
session.proxies = {...} # Here I want to add a single proxy
目前我正在遍历一堆代理,每次迭代都会创建一个新会话。我只想为每次迭代设置一个代理。
我在文档中看到的唯一示例是:
proxies = {
"http": "http://10.10.1.10:3128",
"https": "http://10.10.1.10:1080",
}
requests.get("http://example.org", proxies=proxies)
我已尝试遵循此方法,但无济于事。这是我的脚本代码:
# eg. line = 59.43.102.33:80
r = s.get('http://icanhazip.com', proxies={'http': 'http://' + line})
但是我得到一个错误:
requests.packages.urllib3.exceptions.LocationParseError: Failed to parse 59.43.102.33:80
如何在会话对象上设置单个代理?
其实你是对的,但是你一定要保证你定义的'line',这个我试过了,可以的:
>>> import requests
>>> s = requests.Session()
>>> s.get("http://www.baidu.com", proxies={'http': 'http://10.11.4.254:3128'})
<Response [200]>
你是不是定义了line = ' 59.43.102.33:80'
这样的行,地址前面有一个space。
除了@neowu' 的回答,如果你想为会话对象的生命周期设置代理,你还可以执行以下操作 -
import requests
proxies = {'http': 'http://10.11.4.254:3128'}
s = requests.session()
s.proxies.update(proxies)
s.get("http://www.example.com") # Here the proxies will also be automatically used because we have attached those to the session object, so no need to pass separately in each call
希望这可能会导致答案:
urllib3.util.url.parse_url(url)
Given a url, return a parsed Url namedtuple. Best-effort is performed to parse incomplete urls. Fields not provided will be None.
除了目前已有的解决方案外,您还可以通过其他方式设置代理:
import requests
with requests.Session() as s:
# either like this
s.proxies = {'https': 'http://105.234.154.195:8888', 'http': 'http://199.188.92.69:8000'}
# or like this
s.proxies['https'] = 'http://105.234.154.195:8888'
r = s.get(link)
我正在使用 Python 请求包发送 http 请求。我想向请求会话对象添加一个代理。例如
session = requests.Session()
session.proxies = {...} # Here I want to add a single proxy
目前我正在遍历一堆代理,每次迭代都会创建一个新会话。我只想为每次迭代设置一个代理。
我在文档中看到的唯一示例是:
proxies = {
"http": "http://10.10.1.10:3128",
"https": "http://10.10.1.10:1080",
}
requests.get("http://example.org", proxies=proxies)
我已尝试遵循此方法,但无济于事。这是我的脚本代码:
# eg. line = 59.43.102.33:80
r = s.get('http://icanhazip.com', proxies={'http': 'http://' + line})
但是我得到一个错误:
requests.packages.urllib3.exceptions.LocationParseError: Failed to parse 59.43.102.33:80
如何在会话对象上设置单个代理?
其实你是对的,但是你一定要保证你定义的'line',这个我试过了,可以的:
>>> import requests
>>> s = requests.Session()
>>> s.get("http://www.baidu.com", proxies={'http': 'http://10.11.4.254:3128'})
<Response [200]>
你是不是定义了line = ' 59.43.102.33:80'
这样的行,地址前面有一个space。
除了@neowu' 的回答,如果你想为会话对象的生命周期设置代理,你还可以执行以下操作 -
import requests
proxies = {'http': 'http://10.11.4.254:3128'}
s = requests.session()
s.proxies.update(proxies)
s.get("http://www.example.com") # Here the proxies will also be automatically used because we have attached those to the session object, so no need to pass separately in each call
希望这可能会导致答案:
urllib3.util.url.parse_url(url) Given a url, return a parsed Url namedtuple. Best-effort is performed to parse incomplete urls. Fields not provided will be None.
除了目前已有的解决方案外,您还可以通过其他方式设置代理:
import requests
with requests.Session() as s:
# either like this
s.proxies = {'https': 'http://105.234.154.195:8888', 'http': 'http://199.188.92.69:8000'}
# or like this
s.proxies['https'] = 'http://105.234.154.195:8888'
r = s.get(link)