python爬虫系列一:爬取糗百成人的妹子图片(urllib2)
来源:互联网 发布:手机编图软件 编辑:程序博客网 时间:2024/04/30 13:57
撸叼屎是我朋友,单身已久,每天让我给介绍妹子,于是我写了一个python爬虫来暂时满足他。
给撸叼屎的程序
#!/usr/bin/env python# -*- coding: utf-8 -*-from urllib2 import Request, URLError, urlopenimport reimport urllibimport osdef get_url_content(url, retry_times=2): print 'Downloading: ', url try: send_headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; rv:16.0) Gecko/20100101 Firefox/16.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Connection': 'keep-alive' } req = Request(url, headers=send_headers) html_content = urlopen(req).read().decode('gbk', 'ignore').encode('utf-8') # 此处对中文字符进行了转码 except URLError, e: print e.reason html_content = None print "retry times:", retry_times if retry_times > 0: if hasattr(e, 'code') and 500 <= e.code < 600: get_url_content(url, retry_times - 1) return html_contentdef get_pic_url(html_content): pic_reg = 'src="(http://.*?(png|jpg|gif))"' patten = re.compile(pic_reg, re.IGNORECASE) return patten.findall(html_content)def save_pic_urllib(save_path, pic_url): save_pic_name = save_path + pic_url.split('/')[len(pic_url.split('/')) - 1] if not os.path.exists(save_pic_name): print save_pic_name urllib.urlretrieve(pic_url, save_pic_name)def mkdir(mkdir_path): path = mkdir_path.strip() if not os.path.exists(path): os.makedirs(path) return path# print get_url_content("http://httpstat.us/500")if __name__ == "__main__": save_path = mkdir("C:\\meizi\\") for index in range(1, 755): # 按照ID来爬整个网站 src = "http://www.qiubaichengren.com/%s.html" % (index) url_content = get_url_content(src) if url_content: son_save_path = mkdir(save_path + str(index) + "\\") pic_list = get_pic_url(url_content) for i in range(len(pic_list)): pic_url = pic_list[i][0] save_pic_urllib(son_save_path, pic_url) print '第' + str(index) + '页,爬取完毕。撸叼屎,拿去撸吧!'
整个程序可以直接运行,并且亲自测试,没有下载的图片,出现破损,不能看的问题。图片有点小劲爆
说说写这个小爬虫遇到的小问题,urllib.urlretrieve()保存图片,有无法打开的问题
解决办法:设置了详细的headers。
send_headers = {
‘User-Agent’: ‘Mozilla/5.0 (Windows NT 6.2; rv:16.0) Gecko/20100101 Firefox/16.0’,
‘Accept’: ‘text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8’,
‘Connection’: ‘keep-alive’
}
这个爬虫实现了哪些功能
1.可以按照页面来创建文件夹
2当出现了http_code ,500问题时,自动重新下载2次
3,下载过的图片,自动跳过
4,解决了图片下载不能打开的问题
写在最后:身边有很多单身的优秀程序员,等着单身的你。缘分E-mail:289363142@qq.com
爬取糗百成人的妹子图片(requests+正则)
python爬虫系列三:爬取糗百成人的妹子图片(scrapy框架+正则)
1 0
- python爬虫系列一:爬取糗百成人的妹子图片(urllib2)
- python爬虫系列二:爬取糗百成人的妹子图片(requests+正则)
- python爬虫系列三:爬取糗百成人的妹子图片(scrapy框架+正则)
- python爬虫爬取糗百成人图片多线程版本
- python爬虫爬取糗百成人图片单线程版本
- Python爬虫---------------<妹子图>图片抓取(1)
- Python爬虫Urllib2库的高级用法
- python的urllib2和beautifulsoup编写爬虫
- Python网络爬虫(4)煎蛋网妹子图片抓取
- Python爬虫之——爬取妹子图片
- [python] 第一个爬虫, 爬妹子写真集图片
- Python爬虫实战一 | 抓取取校花网的所有妹子
- python爬虫系列一
- Python爬虫小实践:下载妹子图www.mzitu.com网站上所有的妹子图片,并按相册名字建立文件夹分好文件名
- 爬虫系列17.urllib2模块
- python——图片爬虫:爬取爱女神网站(www.znzhi.net)上的妹子图 基础篇
- python——图片爬虫:爬取爱女神网站(www.znzhi.net)上的妹子图 进阶篇
- 爬虫系列之二 urllib2库的使用上
- [Linux]--处理文件名中的空格问题
- 搭建Qt界面的OpenCV开发环境
- OpenCV | YUV420_NV21 转 BGR
- 数据基本类型
- jQuery 页面当含有多条数据时,跳转到第二页时,无法选择第二页数据记录解决
- python爬虫系列一:爬取糗百成人的妹子图片(urllib2)
- 安卓MediaPlayer和VideoView简单使用
- 【HDU5875】Function(RMQ)
- Some file crunching failed, see logs for details解决办法
- 对今天下午的工作内容的反思
- Android编译错误set but not used [-Werror=unused-but-set-variable]
- 基础软件十年铸一剑,企业级分布式数据库HotDB
- hibernate 参数及返回类型绑定
- 第四周项目5--猴子选大王