128°

python爬虫项目-一见倾心壁纸

方法1

 1 import re
 2 import urllib
 3 import urllib.request
 4 
 5 def getHtml(url):
 6     page = urllib.request.urlopen(url)
 7     html = page.read()
 8     return html
 9 
10 def getImage(html,x):
11     #https://mmbiz.qpic.cn/mmbiz_jpg/ib55rg6wzUc3B16KIY3uU53nkcTTDic8uEA4WWBPaHJ8LpibvAnkpS2FZtyjrv7w7dbEeNrhfvPuuyReNAxsLdgJA/640?wx_fmt=jpeg
12     #https://mmbiz.qpic.cn/mmbiz_jpg/ib55rg6wzUc3B16KIY3uU53nkcTTDic8uEHqocI7r86nehl2NeForAqvcTiaEAIuWjTWPKNXnnXIPuUuqnuJeFKYw/640?wx_fmt=jpeg
13     #此处正则为重点
14     reg = 'data-src="(.*?)"'
15     image = re.compile(reg)
16     imlist = re.findall(reg,html.decode('utf-8'))
17 
18     print(imlist)
19     for i in imlist:
20         print(i)
21         print(x)
22         urllib.request.urlretrieve(i,'%s.jpg' % x)
23         x +=1
24     return x
25 x=1
26 url = 'https://mp.weixin.qq.com/s/MVDcn0O3093OlIhMYkqBIA'
27 html = getHtml(url)
28 x = getImage(html,x)
29 print('下载完成')
30 #下载结果与此.py文件在同一目录

方法2:

 

 

本文转载自博客园,原文链接:https://www.cnblogs.com/xdd1997/p/11962969.html

全部评论: 0

    我有话说: