带有 Python 'Requests' 模块的代理 [英] Proxies with Python 'Requests' module
问题描述
关于优秀的Requests模块的简短而简单的蟒蛇.
Just a short, simple one about the excellent Requests module for Python.
我似乎无法在文档中找到变量代理"应包含的内容.当我向它发送一个带有标准IP:PORT"值的 dict 时,它拒绝了它要求 2 个值.所以,我猜(因为这似乎没有包含在文档中)第一个值是 ip,第二个是端口?
I can't seem to find in the documentation what the variable 'proxies' should contain. When I send it a dict with a standard "IP:PORT" value it rejected it asking for 2 values. So, I guess (because this doesn't seem to be covered in the docs) that the first value is the ip and the second the port?
文档只提到了这一点:
proxys –(可选)字典映射协议到代理的 URL.
proxies – (optional) Dictionary mapping protocol to the URL of the proxy.
所以我尝试了这个……我该怎么办?
So I tried this... what should I be doing?
proxy = { ip: port}
我应该在将它们放入字典之前将它们转换为某种类型吗?
and should I convert these to some type before putting them in the dict?
r = requests.get(url,headers=headers,proxies=proxy)
推荐答案
proxies
' dict 语法是 {"protocol":"ip:port", ...}代码>.有了它,您可以为使用 http、https 和 ftp 协议的请求指定不同(或相同)的代理:
The proxies
' dict syntax is {"protocol":"ip:port", ...}
. With it you can specify different (or the same) proxie(s) for requests using http, https, and ftp protocols:
http_proxy = "http://10.10.1.10:3128"
https_proxy = "https://10.10.1.11:1080"
ftp_proxy = "ftp://10.10.1.10:3128"
proxyDict = {
"http" : http_proxy,
"https" : https_proxy,
"ftp" : ftp_proxy
}
r = requests.get(url, headers=headers, proxies=proxyDict)
Deduced from the requests
documentation:
参数:method
– 新请求对象的方法.url
– 新请求对象的 URL.
...proxys
–(可选)字典映射协议到代理的URL.
...
Parameters:
method
– method for the new Request object.
url
– URL for the new Request object.
...
proxies
– (optional) Dictionary mapping protocol to the URL of the proxy.
...
<小时>
在 linux 上,您也可以通过 HTTP_PROXY
、HTTPS_PROXY
和 FTP_PROXY
环境变量执行此操作:
On linux you can also do this via the HTTP_PROXY
, HTTPS_PROXY
, and FTP_PROXY
environment variables:
export HTTP_PROXY=10.10.1.10:3128
export HTTPS_PROXY=10.10.1.11:1080
export FTP_PROXY=10.10.1.10:3128
在 Windows 上:
On Windows:
set http_proxy=10.10.1.10:3128
set https_proxy=10.10.1.11:1080
set ftp_proxy=10.10.1.10:3128
<小时>
谢谢,杰指出这一点:
语法随 requests 2.0.0 发生变化.
您需要向 url 添加架构:https://2.python-requests.org/en/latest/user/advanced/#proxies
这篇关于带有 Python 'Requests' 模块的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!