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)
原文地址:https://www.cnblogs.com/Traveller-Lee/p/8954483.html