是否可以使用python请求执行代理链接?

时间:2014-11-28 07:40:46

标签: python proxy python-requests

是否可以使用python requests执行代理链接?或者是否有任何外部支持来实现这一点,可能仍然使用requests

我的意思是:

r = requests.get(url=url, proxies={'http': [proxy_1, ..., proxy_n]})

以便请求通过每个代理。

1 个答案:

答案 0 :(得分:-1)

有很多方法,我使用下面的方法,

import collections, requests

def ChainProxy(a,proxies,url):
    r=a
    while True:
        try:
            r=r+1
            d = collections.deque(proxies)
            d.rotate(r)
            pp = list(d)
            pp = pp[0]
            stuff = requests.get(url, proxies={'http': 'http://'+pp}).content
            break
        except:
            pass
    return (r,stuff)

你使用它的方式是,

proxies = [a,b,c]
r=0

#And then repeat this everytime,
r,stuff=ChainProxy(r,proxies,url)

每次为您提供一个新的r,并为您提供stuff

页面来源

我已经实施了tryexcept,因为我总是使用免费代理,其中很多人都不工作:-)如果您信任您的代理,您可以跳过该步骤。

希望有所帮助: - )

相关问题