Python requests ip代理爬虫报错 HTTPSConnectionPool(host=‘xxxxx‘, port=443): Max retries exceed...

  • 本人系统:macOS10.15.6 Catalina
  • 场景:使用Python requests 包+ip代理池爬取网站数据
  • 出现报错:HTTPSConnectionPool(host=‘xxxxx’, port=443): Max retries exceeded with url:xxxxxxxx (Caused by Ne…

具体情况下面这个博主已经基本提到了:

点击这里

但是依然出现这个报错。


import time
import random
import requests


USER_AGENTS = [
    "Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5",
    "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0b13pre) Gecko/20110307 Firefox/4.0b13pre",
    "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
    "Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10"
]

headers = {
   "User-Agent": ""
}
# 借助上面的USER-AGENT反爬
s = requests.session()
s.keep_alive = False
requests.adapters.DEFAULT_RETRIES = 10

url = "https://baike.baidu.com/item/人工智能/9180?fromtitle=AI&fromid=25417&fr=aladdin"
for i in range(10):
    proxys = {
    # news_ip是已经读取好的ip 就不放上面代码了
        "https": "http://"+ new_ips[i],
        "http": "http://" + new_ips[i]
    }
    headers['User-Agent'] = random.choice(USER_AGENTS)
    print(proxys)
    print(headers['User-Agent'])
    req = requests.get(url, headers=headers, verify = False, proxies = proxys, timeout = 20).content.decode('utf-8')
    print(req)
    
    time.sleep(5)

首先需要确认自己的的IP地址是否可行。这里参考
传送门
给出的方式,确认不是IP不可行的问题。

后来在知乎上看到有人在传入proxy给proxies的时候,

将 字 典 中 的 " h t t p s " 和 " h t t p " 全 部 大 写 了 , 尝 试 之 后 确 实 可 行 了 将字典中的"https"和"http"全部大写了,尝试之后确实可行了 "https""http".

for i in range(10):
    proxys = {
        "HTTPS": "HTTP://"+ new_ips[i],
        "HTTP": "HTTP://" + new_ips[i]
        # 在这里全部大写了!
        
    }
    headers['User-Agent'] = random.choice(USER_AGENTS)
    print(proxys)
    print(headers['User-Agent'])
    req = requests.get(url, headers=headers, verify = False, proxies = proxys, timeout = 20).content.decode('utf-8')
    print(req)
    
    time.sleep(5)

记录一下今晚踩的几个雷:

  • 注意字典proxy中,对于每个value无论key是HTTP还是HTTPS,都用HTTP开头!只有key用HTTPS!
  • 如果requests想要爬取的网站是https:// ,那么一定一定需要在requests里加上verify = False这句话
  • 这里是我用的免费ip代理池,勉强能用。下面把这个网站的爬取ip的代码放在这:
# 获取可用的IP代理
import re
import requests
header = {
#     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    "User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36" 
}

response = requests.get("http://www.66ip.cn/areaindex_11/1.html", headers = header)

encode_content = response.content.decode('gb18030', 'replace')

ips = re.findall("<td>(?:[0-9]{1,3}\.){3}[0-9]{1,3}</td><td>[0-9]{1,5}</td>", encode_content)
new_ips = []
# proxies = []

for i in ips:
#     print(i)
    new_ips.append(i.replace("</td><td>", ":"))
#     print(i)
                   
for i in range(len(new_ips)):
    new_ips[i] = new_ips[i].replace("<td>", "")
    new_ips[i] = new_ips[i].replace("</td>", "")
for i in new_ips:
    print(i)


  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
python编写的爬虫代理ip池.zip 运行环境 python3 和mysql数据库 下载使用 下载源码 git clone .git 或者在下载zip文件 安装依赖 pip install -i https://pypi.douban.com/simple/ -r requments.txt 创建数据库 mysql -uroot -p create database ippool charset=utf8; 配置项目 # ProxyIPPool/settings.py 基本的配置文件 # Database 使用mysql DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'ippool', # db name 'USER': 'root', # 用户名 'PASSWORD': 'password', # 密码 'HOST': 'localhost', 'PORT': 3306, } } # uwsgi.ini uwsgi.ini [uwsgi] # 监听的ip地址和端口 这里修改访问端口 http=0.0.0.0:8000 # 配置工程目录 项目所在的绝对路径 chdir=/path/to/proxy_ip_pool/ProxyIPPool/ # 配置项目的wsgi目录。相对于工程目录 wsgi-file=ProxyIPPool/wsgi.py 生成迁移文件和执行迁移文件 python manage.py makemigrations python manage.py migrate 启动 方法一 cd ProxyIPPool # 进入到manage.py这一级 python manage.py runserver # 启动后访问http://127.0.0.1:8000 方式二 # 使用uwgi 启动服务 这样可以后台启动 uwsgi --ini uwsgi.ini 可以使用方式一进行调试运行,方式二进行稳定运行 启动爬取代理ip的脚本 # 调试时运行 python run.py # 在服务器中可以运行  nohup python -u run.py >> crawler.out 2>&1 & 注意在项目下创建存储日志的文件 /ProxyIPPool/log/log.txt API接口 请求方式GET http://{运行服务器的ip}/api/fetch/ 随机返回一个代理ip信息 http://{运行服务器的ip}/api/random/{个数}, 随机返回指定个数 首页展示的内容可以在这里IPPool/views.py中修改 # IPPool/views.py # 修改context 改变返回首页的内容 def index(requests): """ 返回到说明页 :param requests: :return: """ context = '<h3>1.访问接口http://{运行服务器的ip}/api/fetch/ 随机返回一个代理ip信息</h3> <br/>' \ '<h3>2.访问接口http://{运行服务器的ip}/api/random/{个数}, 随机返回指定个数</h3> <br/>' return HttpResponse(context)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值