代码运行后报错
代码报错。运行后只能保存5张,而且第5张打不开。另外图片与URL中的不一样。
Windows、paycharm、python2.7.5(安装其他软件时候自带的)
import urllib2 import re req = urllib2.urlopen('http://www.imooc.com/course/list') buf = req.read() listurl = re.findall(r'http://.+\.jpg', buf) i=0 for url in listurl: f = open('D:/0/' + str(i) + '.jpg', 'wb') req = urllib2.urlopen(url) buf = req.read() f.write(buf) f.close() i += 1
错误信息
Traceback (most recent call last): File "D:/02/python/imooc.py", line 12, in <module> req = urllib2.urlopen(url) File "C:\Python27\ArcGIS10.2\lib\urllib2.py", line 127, in urlopen return _opener.open(url, data, timeout) File "C:\Python27\ArcGIS10.2\lib\urllib2.py", line 410, in open response = meth(req, response) File "C:\Python27\ArcGIS10.2\lib\urllib2.py", line 523, in http_response 'http', request, response, code, msg, hdrs) File "C:\Python27\ArcGIS10.2\lib\urllib2.py", line 448, in error return self._call_chain(*args) File "C:\Python27\ArcGIS10.2\lib\urllib2.py", line 382, in _call_chain result = func(*args) File "C:\Python27\ArcGIS10.2\lib\urllib2.py", line 531, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 400: Bad Request