CRAWL问题:一个关于计数请求的问题



我试图在使用请求时设置随机代理,但遇到了一些问题。这是我的代码:

import requests
import random
pool = ['220.186.175.252:4216','106.110.39.106:4232']
proxy={'https':random.choice(pool)}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
test_url = "http://httpbin.org/ip" # a url test ip
response = requests.get(url=test_url,headers=headers,proxies=proxy)
text = response.text
print(text)

结果:

{"origin": "112.10.164.203"}

它不起作用,所以我尝试更改我的代理,我想也许它使用http而不是https,我将代理更改为:

proxy={'https':random.choice(pool)}

不幸的是,我得到了一个错误:

requests.exceptions.ProxyError: HTTPConnectionPool(host='106.110.39.106', port=4232): Max retries exceeded with url: http://httpbin.org/ip (Caused by ProxyError('Cannot connect to proxy.', RemoteDisconnected('Remote end closed connection without response',)))

所以,我有两个问题:1.如何在请求中设置随机代理2.为什么我更改代理获取此错误

如果你能解决我的问题,我很高兴!

您做得对,出现此错误的原因是您的代理不支持http请求。在使用它之前,你需要知道它支持什么样的协议。免费代理列表

这就是我定义随机代理的方式

import requests
import random
https = ['220.186.175.252:4216','106.110.39.106:4232']
http = ["169.50.180.250:3128"]
proxy={'https':random.choice(https),"http":random.choice(http)}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
test_url = "http://httpbin.org/ip" # a url test ip
response = requests.get(url=test_url,headers=headers,proxies=proxy)
text = response.text
print(text)

最新更新