Python网络爬虫——爬取网站图片小工具

  • Post author:
  • Post category:python




最近初学python爬虫,就写了一个爬取网站图片的小工具,界面如下:




用到的包主要是爬虫常用的urllib,urllib2和图形界面用的Tkinter,完整代码如下:


# -*- coding:utf-8 -*-
# coding=UTF-8
 
import os,urllib,urllib2,re
from Tkinter import *
import tkMessageBox
import tkFileDialog

url = u"https://www.baidu.com/"
path = "D:\图片采集/"

def getHtml(url):
	webfile = urllib.urlopen(url)
	outhtml = webfile.read()
	print outhtml
	return 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 imgList
	return imgList
 
def download(imgList, page):
	x = 1
	for 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+' >> '+filepathname
		urllib.urlretrieve(imgurl,filepathname)
		x+=1
 
def downImageNum(pagenum):
	page = 1
	pageNumber = pagenum
	while(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 url
		global path
		global outpath
		url=self.urlInput.get() or 'url'
		path=self.pathInput.get() or 'path'
		if url=='url':
			tkMessageBox.showinfo('Message','请输入需要采集图片的网址。' )
			return
		if path=='path':
			tkMessageBox.showinfo('Message','请输入或选择存储图片的本地文件夹。' )
			return
		outpath = path.encode('gbk')
		#outpath = unicode(path, "utf-8").encode('gbk')
		if os.path.exists(outpath):
			pass
		else: 
			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




















版权声明:本文为hungerliu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。