爬虫 遇到请求异常requests.exceptions.ConnectionError该如何解决

跟着网上教程学爬虫的时候,写了跟视频一摸一样的代码,但是遇到了以下报错,尝试用了{“connection”:"close"} 还是没用

报错如下:
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

requests.exceptions.ConnectionError: HTTPConnectionPool(host='scxk.nmpa.gov.cn', port=81): Max retries exceeded with url: /xk/itownet/portalAction.do?method=getXkzsList (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000294E00AC0A0>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))

可能的原因:网络不好,超时断开,或者对方服务器有限制爬虫,你慢一点爬,过一会重试,换一个ip

建议你尝试下,打开vpn的全局代理,重启jupyter notebook,因为PAC智能代理并不能在程序中使用。
具体参考: 用python对twitter进行数据挖掘之TimeoutError_baoFeng_Li的博客-CSDN博客 问题一:TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connectio n failed because connected host h... https://blog.csdn.net/CrozonKDD/article/details/80417883

请求次数过多 被远程服务器拒绝连接了

在每个for循环中添加time.sleep(10), 可以参考 【小白入门】用Python增加文章阅读量_202xxx的博客-CSDN博客 安装requestspip install requests启动代码import requestsimport timedef get_html(html_url): headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36' } re https://blog.csdn.net/weixin_37474682/article/details/119935044

ip被封了
做个免费的代理池 或者使用付费代理
免费代理池: python3-爬虫 07 一个简单的代理池_今天我牙疼的博客-CSDN博客 一个简单的代理池使用redis数据库存储,使用时需要在setting文件中修改相应的信息共包含: crawler.py: 获取模块,负责从以下两个网站爬取免费代理 http://www.goubanjia.com http://www.66ip.cn util.py: 一个工具类,用于爬取免费代理 redisSave.py:存储模块, 将爬取到的代理存入数据库 getter.py: 辅助存储爬到的代理 tester.py: 测试模块,测试代理的可用性 api.py: 接口 https://blog.csdn.net/qq_40924514/article/details/109694920

麻烦你把网线接上。哈哈