weixin_45495338 2020-02-17 17:53 采纳率: 0%
浏览 5592

Python requests模块爬取https网站使用代理的问题

请问在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] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。')))
  • 写回答

1条回答 默认 最新

  • 关注
    评论

报告相同问题?

悬赏问题

  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿