qq_一口一个小朋友_0
2017-08-02 14:28
代码报错。运行后只能保存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
listurl = re.findall(r'http.+?\.jpg', buf) 改成非贪婪模式就行了
求解:我的情况跟你一样,改成非贪婪模式后确实也可以了。但我想知道为什么贪婪模式下不行,非贪婪就可以了呢
python正则表达式
80575 学习 · 174 问题
相似问题