请问在Python使用requests库做爬虫爬取https协议的网站的时候,代理要怎么设置呢?
官方文档的说明如下
import requests
proxies = {
"http": "http://10.10.1.10:3128",
"https": "http://10.10.1.10:1080",}
requests.get("http://example.org", proxies=proxies)
我的第一个问题是,为什么 "https": "http://10.10.1.10:1080" 这里对应的ip前面不是https而也是用http呢?
另外,我爬取了http://www.xsdaili.com
上的代理,按照上面的方式通过访问https://httpbin.org/get
验证,但是结果都是代理无法访问(代码如下,d_ip是示例,实际抓取的ip请见附件)不知道是不是代理的用法有问题。还是所有的代理都无效...
编程新手,对于web相关知识也很有限。希望各位能帮忙解答。
谢谢!
import random
import requests
d_ip = {"https": [
"47.112.200.175:8000",
"121.237.149.33:3000",
"123.149.136.43:9999"]}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36'}
proxies = {"https" : "http://" + random.choice(d_ip["https"])}
r = requests.get("https://httpbin.org/get", headers=headers, proxies=proxies)
# 错误如下
ProxyError: HTTPSConnectionPool(host='httpbin.org', port=443): Max retries exceeded with url: /get (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x000001E87F7E7F28>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。')))