python爬虫之爬取网页
2016-06-12 21:31
399 查看
本文代码源自 Python爬虫抓取网页图片
希望读者点击原文进行阅览,本人使用Python2.7+Pycharm编译正常
本文采用Markdown格式编写
这是百度之后得到的一个有用的代码,测试一下正常使用
很显然,我们对百度贴吧的图片没什么意向,新浪博客首页经常有个拍妹子的博客,我就盯上那个了,慢慢改变一下代码,学习一下
希望读者点击原文进行阅览,本人使用Python2.7+Pycharm编译正常
本文采用Markdown格式编写
# -*- coding: utf-8 -*- import urllib import re import time import os #显示下载进度 def schedule(a,b,c): ''''' a:已经下载的数据块 b:数据块的大小 c:远程文件的大小 ''' per = 100.0 * a * b / c if per > 100 : per = 100 print '%.2f%%' % per def getHtml(url): page = urllib.urlopen(url) html = page.read() return html def downloadImg(html): reg = r'src="(.+?\.jpg)" pic_ext' imgre = re.compile(reg) imglist = re.findall(imgre, html) #定义文件夹的名字 t = time.localtime(time.time()) foldername = str(t.__getattribute__("tm_year"))+"-"+str(t.__getattribute__("tm_mon"))+"-"+str(t.__getattribute__("tm_mday")) picpath = 'D:\\ImageDownload\\%s' % (foldername) #下载到的本地目录 if not os.path.exists(picpath): #路径不存在时创建一个 os.makedirs(picpath) x = 0 for imgurl in imglist: target = picpath+'\\%s.jpg' % x print 'Downloading image to location: ' + target + '\nurl=' + imgurl image = urllib.urlretrieve(imgurl, target, schedule) x += 1 return image; if __name__ == '__main__': print ''' ************************************* ** Welcome to use Spider ** ** Created on 2014-05-13 ** ** @author: cruise ** *************************************''' html = getHtml("http://tieba.baidu.com/p/2460150866") downloadImg(html) print "Download has finished."
这是百度之后得到的一个有用的代码,测试一下正常使用
很显然,我们对百度贴吧的图片没什么意向,新浪博客首页经常有个拍妹子的博客,我就盯上那个了,慢慢改变一下代码,学习一下
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法