Python网络爬虫——爬取网站图片小工具
来源:互联网 发布:数据权限清理方案 编辑:程序博客网 时间:2024/05/16 08:15
最近初学python爬虫,就写了一个爬取网站图片的小工具,界面如下:
用到的包主要是爬虫常用的urllib,urllib2和图形界面用的Tkinter,完整代码如下:
# -*- coding:utf-8 -*-# coding=UTF-8 import os,urllib,urllib2,refrom Tkinter import *import tkMessageBoximport tkFileDialogurl = u"https://www.baidu.com/"path = "D:\图片采集/"def getHtml(url):webfile = urllib.urlopen(url)outhtml = webfile.read()print outhtmlreturn outhtml def getImageList(html):restr=ur'('restr+=ur'http:\/\/[^\s,"]*\.jpg'restr+=ur'|http:\/\/[^\s,"]*\.jpeg'restr+=ur'|http:\/\/[^\s,"]*\.png'restr+=ur'|http:\/\/[^\s,"]*\.gif'restr+=ur'|http:\/\/[^\s,"]*\.bmp'restr+=ur'|https:\/\/[^\s,"]*\.jpg' restr+=ur'|https:\/\/[^\s,"]*\.jpeg'restr+=ur'|https:\/\/[^\s,"]*\.png'restr+=ur'|https:\/\/[^\s,"]*\.gif'restr+=ur'|https:\/\/[^\s,"]*\.bmp'restr+=ur')'htmlurl = re.compile(restr)imgList = re.findall(htmlurl,html)print imgListreturn imgList def download(imgList, page):x = 1for imgurl in imgList:filepathname=str(outpath+'/pic_%09d_%010d'%(page,x)+str(os.path.splitext(urllib2.unquote(imgurl).decode('utf8').split('/')[-1])[1])).lower()print '[Debug] Download file :'+ imgurl+' >> '+filepathnameurllib.urlretrieve(imgurl,filepathname)x+=1 def downImageNum(pagenum):page = 1pageNumber = pagenumwhile(page <= pageNumber):html = getHtml(url)#获得url指向的html内容imageList = getImageList(html)#获得所有图片的地址,返回列表download(imageList,page)#下载所有的图片page = page+1 class Application(Frame):def __init__(self,master=None):Frame.__init__(self,master)self.pack()self.createWidgets()def createWidgets(self):self.helloLabel=Label(self,text='请输入需要采集图片的网址(请包含协议头,如"http://"):',height=3)self.helloLabel.pack(side=TOP,fill=X)self.urlInput=Entry(self,width=50)self.urlInput.pack(side=TOP,fill=X)self.pathLabel=Label(self,text='请输入或选择存储图片的本地文件夹:',height=3)self.pathLabel.pack(side=TOP,fill=X)self.pathInput=Entry(self,width=50)self.pathInput.pack(side=TOP)self.selectButton=Button(self,text='选择',command=self.changePath,cursor="hand2") #self.selectButton=Button(self,text='选择',command=self.pathInput.insert(0,tkFileDialog.askopenfilename(initialdir = 'D:/')))self.selectButton.pack(side=TOP,anchor=E)self.alertButton=Button(self,text='开始采集',command=self.hello,cursor="hand2")self.alertButton.pack(side=BOTTOM,expand=YES)def changePath(self):path=tkFileDialog.askdirectory(initialdir = 'D:/')self.pathInput.insert(0,path)#path=tkFileDialog.askopenfilename(initialdir = 'D:/')def hello(self):global urlglobal pathglobal outpathurl=self.urlInput.get() or 'url'path=self.pathInput.get() or 'path'if url=='url':tkMessageBox.showinfo('Message','请输入需要采集图片的网址。' )returnif path=='path':tkMessageBox.showinfo('Message','请输入或选择存储图片的本地文件夹。' )returnoutpath = path.encode('gbk')#outpath = unicode(path, "utf-8").encode('gbk')if os.path.exists(outpath):passelse: os.makedirs(outpath)if __name__ == '__main__':downImageNum(1)tkMessageBox.showinfo('Message','来自"%s"的图片内容已经采集完毕,已存储到"%s"的文件夹内。' % (url,path.encode('utf-8')))app=Application()app.master.title('图片抓取小工具')app.master.geometry('500x300+500+200')#app.master.iconbitmap('C:\Users\Administrator\Desktop\python抓取图片\picture_pub_24px_546427_easyicon.net.ico')app.mainloop()
PS:代码中注释掉的内容为有问题或扩展内容,大家可自行参考。
另外:我还用py2exe打包出了一份exe,可以在win环境下直接用,不需要装python环境。
下载地址:
http://download.csdn.net/detail/hungerliu/9805401
0 0
- Python网络爬虫——爬取网站图片小工具
- Python爬虫——爬取网站的图片
- Python——网络爬虫(爬取网页图片)
- python爬虫爬取网站图片
- python网络爬虫爬取图片代码
- Python——图片转字符小工具
- Python网络爬虫入门(一)——爬取百度贴吧图片
- Python 网络爬虫 011 (高级功能) 支持代理proxy — 让爬虫可以爬取google,Youtube等网站
- Python3 大型网络爬虫实战 003 — scrapy 大型静态图片网站爬虫项目实战 — 实战:爬取 169美女图片网 高清图片
- Python可自动登录爬取图片的网络爬虫
- Python网络爬虫requests、bs4爬取空姐网图片
- Python爬虫——爬取网页中的图片小试牛刀
- Python 爬虫实例——网页图片爬取
- Python爬虫之——爬取妹子图片
- Python爬虫入门系列之——爬取图片
- PYTHON爬虫——必应图片关键词爬取
- PYTHON爬虫——必应图片关键词爬取
- Python网络爬虫——爬取POJ题目
- K-Means ++ 算法
- Oracle SQL如何判断一个字段值的长度
- Spring配置文件中的<beans>标签
- EWSA破解WPA无线密码具体图文教程
- PAT_1018. Public Bike Management
- Python网络爬虫——爬取网站图片小工具
- Spring API中文版
- UE4 不同版本编译常见错误及解决方法
- Linux下用KVM虚拟Mac OS X Sierra系统
- 远程推送
- 2.6.x内核usb转串口FTDI驱动使用
- 说说 JavaScript 中 BOM 的 window 对象
- 树莓派魔镜总结(包括语音和图像识别)
- 互联网协议