Python爬虫(二)图片下载爬虫
2016-08-19 15:55
323 查看
# _*_ encoding:utf-8 _*_ """ 图片下载爬虫 """ import re import urllib2 import urllib #获取网页内容 def get_content(url): html=urllib2.urlopen(url) content=html.read() html.close() return content #根据网页内容,通过正则获取图片url,最后下载 def get_images(info): """ <img class="BDE_Image" src="http://imgsrc.baidu.com/forum/w%3D580/ sign=a66d6c61d60735fa91f04eb1ae500f9f/cc1ebe096b63f6246615f7798544ebf81a4ca305.jpg" pic_ext="jpeg" width="490" height="777"> :param info: :return: """ #首先观察html文件,找到一个规则 #然后用正则表达式来表示这个规则 regex=r'img class="BDE_Image" src="(.+?\.jpg)" ' #.匹配除换行符之外的任意单个字符 #+:表示匹配一次或多次 #?:表示贪婪模式,尽可能少的匹配 pat=re.compile(regex) images_code=re.findall(pat,info) #将图片保存到本地 i=0 for images_url in images_code: print images_url urllib.urlretrieve(images_url,'%s.jpg'%i)#保存在本地,参数1:图片地址,2:图片 i+=1 return images_code url="http://tieba.baidu.com/p/2772656630" info=get_content(url) images=get_images(info)#images是一个列表类型 #print len(images)#输出列表的长度
如果您喜欢我写的博文,读后觉得收获很大,不妨小额赞助我一下,让我有动力继续写出高质量的博文,感谢您的赞赏!!!
相关文章推荐
- python 百度贴吧爬虫(下载图片)
- 我的第一个python爬虫程序(从百度贴吧自动下载图片)
- 每天一篇python:简单爬虫下载图片篇
- python简单的图片下载小爬虫
- Python 爬虫5——爬取并下载网页指定规格的图片
- Python 爬虫学习 网页图片下载
- python爬虫 分页获取图片并下载
- Python爬虫下载网站上的图片到本地
- Python 3 爬虫之批量下载字帖图片
- Python简单知乎爬虫--爬取页面的图片并下载到本地
- python爬虫图片下载
- python实现爬虫下载美女图片
- [python][爬虫]从网页中下载图片
- python爬虫(1)下载任意网页图片
- python中使用网络爬虫下载图片
- python 爬虫学习<将某一页的所有图片下载下来>
- python实现爬虫下载美女图片
- python实现爬虫下载美女图片
- Python 爬虫5——爬取并下载网页指定规格的图片
- python爬虫框架scrapy学习图片下载