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

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

# -*- coding:utf-8 -*-
# coding=UTF-8import os,urllib,urllib2,re
from Tkinter import *
import tkMessageBox
import tkFileDialogurl = u"https://www.baidu.com/"
path = "D:\图片采集/"def getHtml(url):webfile = urllib.urlopen(url)outhtml = webfile.read()print outhtmlreturn outhtmldef 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 imgListdef 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+=1def downImageNum(pagenum):page = 1pageNumber = pagenumwhile(page <= pageNumber):html = getHtml(url)#获得url指向的html内容imageList = getImageList(html)#获得所有图片的地址,返回列表download(imageList,page)#下载所有的图片page = page+1class 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

Python网络爬虫——爬取网站图片小工具相关推荐

  1. Python:网络爬虫爬取某表情包网站

    Python:网络爬虫爬取某表情包网站 参考: 爬虫基础知识点汇总(html文件基础和4个常用库 超级详细长文预警) [爬虫教程]吐血整理,最详细的爬虫入门教程~ HTML的简单介绍 HTML的全称为 ...

  2. 用python网络爬虫爬取英雄联盟英雄图片

    用python爬虫爬取lol皮肤. 这也用python网络爬虫爬取lol英雄皮肤,忘了是看哪个大神的博客(由于当时学了下就一直放在这儿,现在又才拿出来,再加上马上要考二级挺忙的.),代码基本上是没改, ...

  3. 如何利用Python网络爬虫爬取微信朋友圈动态--附代码(下)

    前天给大家分享了如何利用Python网络爬虫爬取微信朋友圈数据的上篇(理论篇),今天给大家分享一下代码实现(实战篇),接着上篇往下继续深入. 一.代码实现 1.修改Scrapy项目中的items.py ...

  4. python朋友圈动态_如何利用Python网络爬虫爬取微信朋友圈动态--附代码(下)

    前天给大家分享了如何利用Python网络爬虫爬取微信朋友圈数据的上篇(理论篇),今天给大家分享一下代码实现(实战篇),接着上篇往下继续深入. 一.代码实现 1.修改Scrapy项目中的items.py ...

  5. python抓取朋友圈动态_如何利用Python网络爬虫爬取微信朋友圈动态--附代码(下)...

    原标题:如何利用Python网络爬虫爬取微信朋友圈动态--附代码(下) 前天给大家分享了如何利用Python网络爬虫爬取微信朋友圈数据的上篇(理论篇),今天给大家分享一下代码实现(实战篇),接着上篇往 ...

  6. Python网络爬虫爬取虎扑步行街爆照区话题

    Python网络爬虫爬取虎扑步行街爆照区话题 作者:郜科科 最近的任务挺多的,但是心情很烦躁,想做一些自己喜欢的东西,前些时候感觉Python爬虫很好玩,就自学了一下.本人比较喜欢逛街--虎扑步行街, ...

  7. 使用python网络爬虫爬取新浪新闻(一)

    使用python网络爬虫爬取新浪新闻 第一次写博客,感觉有点不太习惯!不知道怎么突然就想学学爬虫了,然后就用了一天的时间,跟着教程写了这个爬虫,!不说废话了,我将我从教程上学习的东西整个写下来吧,从头 ...

  8. python如何爬虫网页数据-python网络爬虫爬取网页内容

    1.什么是网络爬虫? 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自 ...

  9. python网络爬虫——爬取嗅事百科

    网络爬虫 - 爬取嗅事百科 爬取过程分三大步: 一,请求,获取网页源码 url为:url =' https : //www.qiushibaike.com/8hr/page/'+ str(page)先 ...

最新文章

  1. 专题 7 设备文件编程
  2. LeetCode--167--两数之和 II - 输入有序数组
  3. 这些实验好习惯科研小白一定要先养成!
  4. 内网穿透 无需公网ip_无需端口映射、无需公网IP,60秒实现FTP服务远程访问
  5. 毕业2年,我的工作小结
  6. 机器学习基础笔记总结
  7. 两台windows服务器----SVN的迁移
  8. 设计模式笔记二十四:模板模式
  9. 自动化系统计算机网络期末考试题,模拟试卷_计算机网络试题B-自动化-孙璐
  10. 哪里找电商素材模板?
  11. 浅谈php之设计模式基础
  12. 修改Maven默认编译级别
  13. 003 Nginx虚拟主机配置
  14. Windows10配置和挂载联想DE2000H存储服务器
  15. 中创向心力:如何把思想政治教育贯穿职业教育全过程?
  16. oracle 客户端的安装失败,关于安装oracle 11g client 出现安装先决条件检查全部失败...
  17. FS68001 全新原装 SOP8 无线充电供电IC 无线充电芯片 无线输电芯片5W方案
  18. yarn : 无法加载文件 D:\nodejs\node_global\yarn.ps1,因为在此系统上禁止运行脚本。
  19. 比心app源码,html 获取时间
  20. 数据中心太耗电,送你一个节能神器

热门文章

  1. 微信小程序——页面引用 模块引用
  2. 转载:奈奎斯特采样定理
  3. Spark DAGScheduler源码分析系列之一: 基础
  4. stm32 串口2空闲中断死机_关于STM32串口空闲中断IDEL的问题
  5. 盘点:U盘损坏后如何修复的五种状况和解决方法
  6. 武汉职业技术学院计算机学院老师,武汉职业技术学院计算机技术与软件工程学院毕业论文(设计)撰写规范...
  7. Mybatis中的常用标签
  8. mybatis的标签以及参数传递
  9. 人大网院计算机在线考试答案,2018年12月计算机二级MSOffice操作题及参考答案
  10. python @property