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
原创粉丝点击