首页 > 编程语言 > 详细

python爬取图片简记

时间:2018-04-26 19:41:01      阅读:195      评论:0      收藏:0      [点我收藏+]
参考: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)

 

python爬取图片简记

原文:https://www.cnblogs.com/Traveller-Lee/p/8954483.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!