Python3下載網頁中的圖片
阿新 • • 發佈:2019-01-26
注意:import urllib.request import re req = urllib.request.urlopen('http://www.imooc.com/') buf = req.read() webstr = buf.decode() urlList = re.findall(r'http:.+\.jpg', webstr); i = 0 for url in urlList: f = open(str(i)+'.jpg','wb') #注意第二個引數要寫成wb,寫成w會報錯 req = urllib.request.urlopen(url) buf = req.read() #bufstr = buf.decode('utf-8','ignore') f.write(buf) i += 1
python 3.x中urllib庫和urilib2庫合併成了urllib庫。。
其中urllib2.urlopen()變成了urllib.request.urlopen()
urllib2.Request()變成了urllib.request.Request()