是否可以使用 python requests 执行代理链接?或者是否有任何外部支持可以实现这一点,可能仍然使用 requests?
我的意思是:
r = requests.get(url=url, proxies={'http': [proxy_1, ..., proxy_n]})
以便请求通过每个代理。
请您参考如下方法:
方法有很多种,我用的是下面的一种,
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
我已经实现了 try 和 except 因为我总是使用免费代理,但其中很多都不起作用 :-) 如果您相信您的代理,您可以跳过这一步代理。
希望对您有所帮助:-)
