Python 如何为请求会话对象设置单个代理?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30837839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set a single proxy for a requests session object?
提问by Torra
I'm using the Python requests package to send http requests. I want to add a single proxy to the requests session object. eg.
我正在使用 Python requests 包发送 http 请求。我想向请求会话对象添加一个代理。例如。
session = requests.Session()
session.proxies = {...} # Here I want to add a single proxy
Currently I am looping through a bunch of proxies, and at each iteration a new session is made. I only want to set a single proxy for each iteration.
目前我正在遍历一堆代理,并且在每次迭代时都会创建一个新会话。我只想为每次迭代设置一个代理。
The only example I see in the documentation is:
我在文档中看到的唯一示例是:
proxies = {
"http": "http://10.10.1.10:3128",
"https": "http://10.10.1.10:1080",
}
requests.get("http://example.org", proxies=proxies)
I've tried to follow this, but to no avail. Here is my code from the script:
我试图遵循这一点,但无济于事。这是我的脚本代码:
# eg. line = 59.43.102.33:80
r = s.get('http://icanhazip.com', proxies={'http': 'http://' + line})
But I get an error:
但我收到一个错误:
requests.packages.urllib3.exceptions.LocationParseError: Failed to parse 59.43.102.33:80
How is it possible to set a single proxy on a session object?
如何在会话对象上设置单个代理?
采纳答案by NeoWu
In fact, you are right, but you must ensure your defination of 'line', I have tried this , it's ok:
事实上,你是对的,但你必须确保你对“线”的定义,我已经试过了,没关系:
>>> import requests
>>> s = requests.Session()
>>> s.get("http://www.baidu.com", proxies={'http': 'http://10.11.4.254:3128'})
<Response [200]>
Did you define the line like line = ' 59.43.102.33:80'
, there is a space at the front of address.
您是否定义了类似的行line = ' 59.43.102.33:80'
,地址前面有一个空格。
回答by Vikas Ojha
In addition to @neowu' answer, if you would like to set a proxy for the lifetime of a session object, you can also do the following -
除了@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
回答by user4642224
Hopefully this may lead to an answer:
希望这可能会导致答案:
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.
urllib3.util.url.parse_url(url) 给定一个 url,返回一个解析后的 Url 命名元组。尽力解析不完整的 url。未提供的字段将为 None。
retrived from https://urllib3.readthedocs.org/en/latest/helpers.html
回答by SIM
There are other ways you can set proxies, apart from the solutions you have got so far:
除了目前已有的解决方案外,您还可以通过其他方式设置代理:
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)