requests.exceptions.ReadTimeout: HTTPConnectionPool(host='img.qqzhi.com', port=80): Read timed out.

news/2024/7/19 9:53:13 标签: python, 爬虫, 数据库

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

"C:\Program Files\Python36\python.exe" C:/Users/Administrator.SC-201612181954/PycharmProjects/untitled2/test1
http://www2.bingfeng.tw/data/attachment/forum/201601/21/150057zygjy5rf2y5spf2y.png
http://i-3.yxdown.com/2016/5/19/b24c1344-5524-4f35-96e2-cd1db694d563.jpg
http://i-3.yxdown.com/2016/5/19/b43738d9-5523-4659-a8fe-19b838650af8.jpg
http://attach10.92wy.com/images/2016/0111/1452497908993e6d86.jpg
http://www2.bingfeng.tw/data/attachment/forum/201601/21/150234lsabgyz2yg00ji00.jpg
http://attach10.92wy.com/images/2016/0111/14524963748832cf37.jpg
http://img.qqzhi.com/upload/img_2_2950581147D1797566349_23.jpg
Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\urllib3-1.22-py3.6.egg\urllib3\connectionpool.py", line 387, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "C:\Program Files\Python36\lib\site-packages\urllib3-1.22-py3.6.egg\urllib3\connectionpool.py", line 383, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Program Files\Python36\lib\http\client.py", line 1331, in getresponse
    response.begin()
  File "C:\Program Files\Python36\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "C:\Program Files\Python36\lib\http\client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Program Files\Python36\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\requests\adapters.py", line 440, in send
    timeout=timeout
  File "C:\Program Files\Python36\lib\site-packages\urllib3-1.22-py3.6.egg\urllib3\connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\Program Files\Python36\lib\site-packages\urllib3-1.22-py3.6.egg\urllib3\util\retry.py", line 357, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "C:\Program Files\Python36\lib\site-packages\urllib3-1.22-py3.6.egg\urllib3\packages\six.py", line 686, in reraise
    raise value
  File "C:\Program Files\Python36\lib\site-packages\urllib3-1.22-py3.6.egg\urllib3\connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "C:\Program Files\Python36\lib\site-packages\urllib3-1.22-py3.6.egg\urllib3\connectionpool.py", line 389, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
  File "C:\Program Files\Python36\lib\site-packages\urllib3-1.22-py3.6.egg\urllib3\connectionpool.py", line 309, in _raise_timeout
    raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='img.qqzhi.com', port=80): Read timed out. (read timeout=10)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Administrator.SC-201612181954/PycharmProjects/untitled2/test1", line 12, in <module>
    pic= requests.get(each, timeout=10)
  File "C:\Program Files\Python36\lib\requests\api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Program Files\Python36\lib\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Program Files\Python36\lib\requests\sessions.py", line 502, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Program Files\Python36\lib\requests\sessions.py", line 612, in send
    r = adapter.send(request, **kwargs)
  File "C:\Program Files\Python36\lib\requests\adapters.py", line 516, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='img.qqzhi.com', port=80): Read timed out. (read timeout=10)

具体学爬虫的过程中遇到了这个问题,按照网络上面的教程,爬图片爬到一半就卡死了,下面是代码

python">import re
import requests

#url = 'http://image.baidu.com/search/flip?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1460997499750_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E5%B0%8F%E9%BB%84%E4%BA%BA'
url = 'http://image.baidu.com/search/flip?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1501470791167_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&ctd=1501470791180%5E00_1899X935&word=%E5%9B%A2%E5%AD%90%E5%B0%91%E5%A5%B3'
html = requests.get(url).text
pic_url = re.findall('"objURL":"(.*?)",',html,re.S)
i = 0
for each in pic_url:
    print(each)
    try:
        pic= requests.get(each, timeout=10)
    except requests.exceptions.ConnectionError:
        print( '【错误】当前图片无法下载')
        continue
    string = 'picture1\\'+str(i) + '.jpg'
    fp = open(string,'wb')
    fp.write(pic.content)
    fp.close()
    i += 1

检查以后发现是百度所在的页面该图片虽然收录在自己的机器上,所以你还能看见,但是实际连接已经失效,只能在缩略图上看到

仔细查看错误信息发现返回的错误信息是

requests.exceptions.ReadTimeout:

而不是代码中的

except requests.exceptions.ConnectionError:

我查了一下这两个错误的区别,发现区别不是很大,一般来说readtimeout出现的原因是该网页点击以后加载特别慢,而后者就是直接显示连接错误类型的,大部分的人在验证这里的连接错误的时候都是同时加的验证(这里也对这个程序原来的作者表示不要偷懒,博客上教东西教一半,太坑爹了)

转载于:https://my.oschina.net/u/3471785/blog/1492631


http://www.niftyadmin.cn/n/686284.html

相关文章

2022最新版Python从入门到精通

总被读者问到&#xff0c;我看完了python入门的书&#xff0c;后面就不知道要学什么了。今天就给你们整理全套入门到进阶的教程。 这套教程非常全面而且详细&#xff0c;从Python入门到Python进阶、Django、Flask等Web框架以及爬虫、数据库、算法与数据结构等方面均有涉及&…

手机做条码扫描传给电脑_智能设备中条码扫描模组是如何运作的呢?

对于条码扫描模组如何工作&#xff0c;很多人都不是很清楚其中的工作原理。了解它之前&#xff0c;要知道它可以干什么&#xff0c;条码扫描模组广泛应用于自动识别领域的核心识别部件&#xff0c;它是对条码扫描器进行二次开发的关键零件之一&#xff0c;具备完整独立的条码扫…

算法第四版Question

1.ECLIPES标准输入流 ①Run As-->Run Configurations-->Commom-->Input File在Input File里面输入要读取的文本文件 这对应着标准入流 ②如果还有arg[0]的就在Arguments里面输入参数就可以了。点Run后控制窗口没东西&#xff0c;需要结束输入流。 ③输入流最后结束按C…

Oracle数据类型,函数与存储过程

字符串类型 固定长度&#xff1a;char nchar n 表示Unicode编码 可变长度: varchar2 nvarchar2 数字类型&#xff1a;number&#xff08;P,S&#xff09;P&#xff1a;整数位数&#xff0c;S小数位数 integer 整数 BINARY_FLOAT 单浮点 …

35个Python实战项目,完整源代码!

收集整理最新36个python实战项目源代码&#xff0c;系统实用&#xff0c;不是普通的小脚本&#xff01; 多看多练是提升代码能力的最佳途径&#xff0c;一起操练起来&#xff01; 视频转字符动画 12306 B站弹幕 B站滑块验证码破解 GUI签名 python爬取并简单分析51job py…

全局变量和局部变量的区别_【C】堆区和栈区的区别

1、栈区&#xff08;stack&#xff09;由编译器自动分配释放 &#xff0c;存放函数的参数值&#xff0c;局部变量的值等&#xff0c;内存的分配是连续的&#xff0c;类似于平时我们所说的栈&#xff0c;如果还不清楚&#xff0c;那么就把它想成数组&#xff0c;它的内存分配是连…

SAP HANA中的存储过程(sql procedure)

语法&#xff1a; CREATE PROCEDURE {schema.}name {({IN|OUT|INOUT} param_name data_type {,...})} {LANGUAGE <LANG>} {SQL SECURITY <MODE>} {READS SQL DATA {WITH RESULT VIEW <view_name>}} AS BEG…

java之 ------ 图形界面(三)

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.TitledBorder;public class UserJFrame extends JFrame implements ActionListener {private int number1; //编号private JTextField text_…