Gihub项目分享 —— Python爬虫获取高清桌面壁纸
2016-02-24 20:13
916 查看
https://github.com/zhongjiajie/crawler-photo
啥都不说,先上地址
本人第一个爬虫程序,不敢和大神相比,只是做学习分享用
# -*- coding:utf-8 -*- ''' 多线程,防止网络阻塞(有超时),伪装IE , 无休眠 ,记录时间 , 爬数据库 ''' import requests import re import time from multiprocessing.dummy import Pool as ThreadPool #从数据库中获取所有图片的URL并生成列表 def GetPictureUrl(): #访问网站的数据库,并获取相应的html url1 = 'http://www.socwall.com/images/wallpapers/' html1 = requests.get(url1).text #正则表达求出相应的图片链接 content1 = re.search('<a href="/views/images/">(.*?)<a href="staging/">',html1,re.S).group(1) content2 = re.findall('<a href="(.*?)">',content1,re.S) #生成网页列表 UrlList = [] for each in content2: url2 = 'http://www.socwall.com/images/wallpapers/' + each UrlList.append(url2) return UrlList #模拟IE下载图片 def DownloadPicture(url): try: head = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko'} html = requests.get(url,headers = head,timeout = 40) #模拟IE下载,40s超时退出 with open(r'E://picture2//%s'%url[41:], 'wb+') as f: f.write(html.content) except: with open(r'failure_url.txt','a') as f: f.write(url + '\n') print 'download ' + url + ' failure!' if __name__ == '__main__': time1 = time.time() #记录开始的时间time1 UrlList = GetPictureUrl() #获取图片的URL pool = ThreadPool(4) #开四线程 results = pool.map(DownloadPicture, UrlList) #多线程下载 pool.close() pool.join() time2 = time.time() #记录结束时间time2 print u'合计耗时: ' + str(time2 - time1) #计算耗时量
特点为:多线程,防止网络阻塞(有超时),伪装IE , 无休眠 ,记录时间 , 爬数据库
PS:因为本人发现网站www.socwall.com中的图片放在开放的数据库中,所以二话不说直接拿来用了,就是直接访问网址http://www.socwall.com/images/wallpapers/爬取,还是一个简单的爬虫。
相关文章推荐
- numpy的random模块
- python __init__.py用途
- python-整理--pip whl命令
- [LeetCode]题解(python):087-Scramble String
- 《大话设计模式》Python版代码实现
- 【Python学习笔记】一个很酷的尾递归优化
- 通过Python获取拉钩招聘网站的公司详细地址
- Python中导入其它模块中的函数
- 一个python 服务器程序性能分析
- Python入门——1
- Learn Python in Y Minutes(有时间再慢慢翻译)
- python实现基础排序算法之(插入排序)
- python模块系列之 - xml
- python导入自定义模块
- Python中的下划线
- python cls 和 self
- Python实现字典依据value排序
- Course1-Python数据类型总结
- python的函数式编程知识小结
- Python语言特性(二)