带有Python'请求'模块的代理 [英] Proxies with Python 'Requests' module
问题描述
关于优秀的请求模块的简短说明Python.
Just a short, simple one about the excellent Requests module for Python.
我似乎无法在文档中找到变量代理"应包含的内容.当我发送带有标准"IP:PORT"值的字典时,它拒绝要求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?
文档仅提及以下内容:
代理-(可选)字典到代理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)
从 requests
文档推导出来:
Deduced from the requests
documentation:
参数:
method
–新Request对象的方法.
url
–新Request对象的URL.
...
proxies
–(可选)将字典映射 协议映射到代理的 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上:
set http_proxy=10.10.1.10:3128
set https_proxy=10.10.1.11:1080
set ftp_proxy=10.10.1.10:3128
谢谢杰伊指出:
语法随请求2.0.0 进行了更改.
您需要将架构添加到url: https://2.python-requests.org/en/latest/user/advanced/#proxies
Thanks, Jay for pointing this out:
The syntax changed with requests 2.0.0.
You'll need to add a schema to the url: https://2.python-requests.org/en/latest/user/advanced/#proxies
这篇关于带有Python'请求'模块的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!