python爬取图片简记
生活随笔
收集整理的這篇文章主要介紹了
python爬取图片简记
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考:https://blog.csdn.net/tanlangqie/article/details/79506543
1 # -*- coding:utf-8 -*- 2 import urllib 3 import urllib.request 4 import re 5 6 def getHtml(url): 7 request = urllib.request.Request(url) 8 response = urllib.request.urlopen(request) 9 html = response.read() 10 return html 11 12 def getImg(html): 13 reg = 'data-original="(.+?\.jpg)"' 14 imgre = re.compile(reg) 15 imglist = re.findall(imgre, html.decode('utf-8')) 16 localpath='G:/photo/' 17 x = 1 18 for imgurl in imglist : 19 urllib.request.urlretrieve(imgurl,localpath+'%s.jpg' % x) 20 print('正在下載第%s張圖片' % x) 21 x+=1 22 if x>20: 23 break 24 return None 25 26 html = getHtml("https://www.zhihu.com/question/27364360") 27 getImg(html)
1 # -*- coding:utf-8 -*- 2 import urllib 3 import urllib.request 4 import re 5 6 def getHtml(url): 7 request = urllib.request.Request(url) 8 response = urllib.request.urlopen(request) 9 html = response.read() 10 return html 11 12 def getImg(html): 13 reg = 'data-original="(.+?\.jpg)"' 14 imgre = re.compile(reg) 15 imglist = re.findall(imgre, html.decode('utf-8')) 16 localpath='G:/photo/' 17 x = 1 18 for imgurl in imglist : 19 urllib.request.urlretrieve(imgurl,localpath+'%s.jpg' % x) 20 print('正在下載第%s張圖片' % x) 21 x+=1 22 if x>20: 23 break 24 return None 25 26 html = getHtml("https://www.zhihu.com/question/27364360") 27 getImg(html)
?
轉載于:https://www.cnblogs.com/Traveller-Lee/p/8954483.html
總結
以上是生活随笔為你收集整理的python爬取图片简记的全部內容,希望文章能夠幫你解決所遇到的問題。