用python实现的一个抓取图片的爬虫
来源:互联网 发布:国泰安数据库介绍 编辑:程序博客网 时间:2024/06/05 10:39
最近学到python的时候学到正则表达式,于是心血来潮就用python写了一个简单抓取网站图片的小爬虫,为此贴上代码来记录自己学习python的过程,同时也希望大家多提出爬虫改进的意见,因为此爬虫只能抓取以http://开头的图片,而像百度图库的图片大多数都不是以http://开头,希望大家能给出意见。
废话不多说,代码上:
version:1.0
#!/usr/bin/pythonimport reimport urllibdef getHtml():url=raw_input('Enter url:')page=urllib.urlopen(url) html=page.read()return htmldef getImgUrl(html):print ''' 1 represents .jpg 2 represents .png 4 represents .gif 3 represents .jpg+.png 5 represents .jpg+.gif 6 represents .png+.gif 7 represents .jpg+.png+.gif ''' imgForm=int(raw_input('Enter the Form of Img:'))imgurllist=[]if imgForm==1:reg=r'http://[^\s]+?\.jpg'imgre=re.compile(reg,re.I)imgurllist=re.findall(imgre,html)elif imgForm==2: reg=r'http://[^\s]+?\.png'imgre=re.compile(reg,re.I)imgurllist=re.findall(imgre,html)elif imgForm==4:reg=r'http://[^\s]+?\.gif'imgre=re.compile(reg,re.I)imgurllist=re.findall(imgre,html)elif imgForm==3: reg=r'(http://[^\s]+?\.(png|jpg))'imgre=re.compile(reg,re.I)imglist=re.findall(imgre,html)imgurllist=[] for x in imglist:imgurllist.append(x[0])elif imgForm==5:reg=r'(http://[^\s]+?\.(gif|jpg))'imgre=re.compile(reg,re.I)imglist=re.findall(imgre,html)imgurllist=[] for x in imglist:imgurllist.append(x[0]) elif imgForm==6:reg=r'(http://[^\s]+?\.(png|gif))'imgre=re.compile(reg,re.I)imglist=re.findall(imgre,html)imgurllist=[] for x in imglist:imgurllist.append(x[0])else: reg=r'(http://.+?\.(png|jpg|jpg))'imgre=re.compile(reg,re.I)imglist=re.findall(imgre,html)imgurllist=[] for x in imglist:imgurllist.append(x[0]) return imgurllistdef downloadImg(url):jpg=0;png=0;gif=0;for imgurl in url:if(re.findall(r'.+\.jpg',imgurl)):urllib.urlretrieve(imgurl,"%s.jpg"%jpg)jpg+=1elif(re.findall(r'.+\.png',imgurl)):urllib.urlretrieve(imgurl,"%s.png"%png)png+=1elif(re.findall(r'.+?\.gif',imgurl)):urllib.urlretrieve(imgurl,"%s.gif"%gif)gif+=1else:print "not picture captured"html=getHtml()imgurl=getImgUrl(html)downloadImg(imgurl)print imgurl
0 0
- 用python实现的一个抓取图片的爬虫
- 用python实现一个抓取电影的爬虫
- Python 实现简单的爬虫功能: 图片的抓取
- Python实现一个简单的图片爬虫
- 简单的python爬虫抓取图片实例
- 爬虫:实现网站的全部图片抓取
- python 实现网站图片抓取小爬虫
- python实现简单爬虫抓取图片
- python爬虫抓取图片
- 基于python的网络爬虫---抓取p站图片
- python爬虫抓取多关键词搜索的百度图片
- python小爬虫—抓取pixabay网站的图片资源
- Python抓取段子的爬虫
- python实现批量图片的抓取
- Python 实现简单的网络抓取图片
- Hello Python!用python写一个抓取CSDN博客文章的简单爬虫
- 用python爬虫抓取知乎图片
- 一个python爬图片的小爬虫
- JIRA测试-workflow初识
- linux蓝牙管理
- 宝箱-UVA-12325
- DAY1-51单片机强化练习-几个样板
- 如何登陆到运行linux的虚拟机
- 用python实现的一个抓取图片的爬虫
- MongoDb的一个实例
- C++ Primer 笔记+习题解答(四)
- mybatis配置懒加载可能导致性能问题
- zookeeper
- 《设计模式之禅》——适配器模式
- Unity3d 游戏与C#服务器 异步Socket 交互 (一)
- hdu4081---Qin Shi Huang's National Road System
- 带自定义属性的view控件