zoukankan      html  css  js  c++  java
  • 工作问题--------爬虫遇到requests.exceptions.ConnectionError: HTTPSConnectionPool Max retries exceeded

    问题描述:爬取京东的网站,爬取一段时间后报错。

    经过一番查询,发现该错误是因为如下:

    1. http的连接数超过最大限制,默认的情况下连接是Keep-alive的,所以这就导致了服务器保持了太多连接而不能再新建连接。

    2. ip被封

    3. 程序请求速度过快。

    解决办法如下:

    第一种方法

    try:
        page1 = requests.get(ap)
    except requests.exceptions.ConnectionError:
        r.status_code "Connection refused"

    第二种方法:

    request的连接数过多而导致Max retries exceeded

    在header中不使用持久连接

    'Connection': 'close'或requests.adapters.DEFAULT_RETRIES = 5

    第三种方法:

    针对请求请求速度过快导致程序报错。

    解决方法可以参考以下例子:

    import time
     
    while 1:
        try:
            page = requests.get(url)
        except:
            print("Connection refused by the server..")
            print("Let me sleep for 5 seconds")
            print("ZZzzzz...")
            time.sleep(5)
            print("Was a nice sleep, now let me continue...")
            continue
    http://www.chenxm.cc/post/536.html

    原文地址:http://www.chenxm.cc/post/536.html

  • 相关阅读:
    设置导航栏标题颜色及字体大小
    FMDB的简单实用
    iPhone越狱
    P1122 最大子树和
    UVA11090 Going in Cycle!!
    P1156 垃圾陷阱
    P1325 雷达安装
    P1038 神经网络
    P2922 [USACO08DEC]秘密消息Secret Message
    P2292 [HNOI2004]L语言
  • 原文地址:https://www.cnblogs.com/huangjiyong/p/12326429.html
Copyright © 2011-2022 走看看