请求 - 代理字典 [英] Requests - proxies dictionary

查看:214
本文介绍了请求 - 代理字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对请求模块感到困惑,特别是代理

I'm little confused about requests module, especially proxies.

来自文件:

PROXIES


字典映射协议到代理的URL(例如{'http':
'foo.bar:3128'})用于每个请求。 / p>

Dictionary mapping protocol to the URL of the proxy (e.g. {‘http’: ‘foo.bar:3128’}) to be used on each Request.

字典中有一种类型的代理吗?我的意思是有可能放在代理列表和请求模块将尝试他们,只使用那些工作?

May there be more proxies of one type in the dictionary? I mean is it possible to put there list of proxies and requests module will try them and use only those which are working?

或者只能有一个代理地址,例如 http

Or there can be only one proxy address for example for http?

推荐答案

使用代理参数受限于python字典(即每个键必须是唯一的)。

Using the proxies parameter is limited by the very nature of a python dictionary (i.e. each key must be unique).

import requests

url = 'http://google.com'
proxies = {'https': '84.22.41.1:3128',
           'http': '185.26.183.14:80',
           'http': '178.33.230.114:3128'}

if __name__ == '__main__':
    print url
    print proxies
    response = requests.get(url, proxies=proxies)
    if response.status_code == 200:
        print response.text
    else:
        print 'Response ERROR', response.status_code

输出

http://google.com
{'http': '178.33.230.114:3128', 'https': '84.22.41.1:3128'}
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for."
...more html...

如您所见,代理中的 http 字典对应于其分配中最后遇到的字符(即 178.33.230.114:3128 )。尝试交换 http

As you can see, the value of the http protocol key in the proxies dictionary corresponds to the last encountered in its assignment (i.e. 178.33.230.114:3128). Try swapping the http entries around.

因此,答案是否定的,您不能使用简单的字典为同一协议指定多个代理。

So, the answer is no, you cannot specify multiple proxies for the same protocol using a simple dictionary.

我已经尝试使用可迭代作为值,这对我有意义。

I have tried using an iterable as a value, which would make sense to me

proxies = {'https': '84.22.41.1:3128',
           'http': ('178.33.230.114:3128', '185.26.183.14:80', )}

但没有运气,会产生错误

but with no luck, it produces an error

这篇关于请求 - 代理字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆