python 爬图 helloworld
2015-07-27 00:37
519 查看
最近发现 吾志 上用户的头像都很个性,另外,对于没有把日记设为私密的用户,最后一天的日记是公开的,谁都可以查看。
所以,如果每天把所有可查看的日记爬一遍,那么~~ 哈哈
以前对爬虫只是了解一点点,没有真的玩过。既然今晚兴致来了,那就随便学一下咯~
参考 http://blog.csdn.net/pleasecallmewhy/article/details/8925978
参考 http://cuiqingcai.com/1052.html
十分简陋,哈哈~
所以,如果每天把所有可查看的日记爬一遍,那么~~ 哈哈
以前对爬虫只是了解一点点,没有真的玩过。既然今晚兴致来了,那就随便学一下咯~
参考 http://blog.csdn.net/pleasecallmewhy/article/details/8925978
参考 http://cuiqingcai.com/1052.html
#coding=utf-8 import os import urllib import urllib2 import re import cookielib def mkdir(path): # 去除左右两边的空格 path = path.strip() # 去除尾部 \ 符号 path = path.rstrip("\\") if not os.path.exists(path): os.makedirs(path) return path def save_file(path, file_name, data): if data == None: return mkdir(path) if (not path.endswith("/")): path = path + "/" f = open(path+file_name, "wb") f.write(data) f.flush() f.close() user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36' headers = {'User-Agent' : user_agent} values = {} data = urllib.urlencode(values) def getHtml(url): req = urllib2.Request(url, data, headers) page = urllib2.urlopen(req, timeout=10) html = page.read() page.close() #print html return html def get_file(url): try: opener = urllib2.build_opener() opener.addheaders = [('User-Agent', 'Mozilla/5.0')] urllib2.install_opener(opener) req = urllib2.Request(url) operate = opener.open(req) data = operate.read() operate.close() return data except BaseException, e: print e, 'fuck' return None def getImg(html): reg = r'src="(.+?\.jpg)" alt=' imgre = re.compile(reg) imglist = re.findall(imgre, html) x = 0 for imgurl in imglist: #urllib.urlretrieve(imgurl, '%s.jpg' % x) da = get_file(imgurl) save_file('.', '%s.jpg' % x, da) x += 1 return x html = getHtml("https://wuzhi.me/last") print getImg(html)
十分简陋,哈哈~
相关文章推荐
- Python中特殊函数集锦
- 在Python中的Django框架中进行字符串翻译
- 在Python的Django框架中创建语言文件
- Python多线程结合队列下载百度音乐的方法
- Python实现删除当前目录下除当前脚本以外的文件和文件夹实例
- python数组过滤实现方法
- python内建函数a-b部分(一)
- python内置函数(二)
- 走进Python世界(五)数据类型 5. 序列类型-字典(Dictionary)
- python系列------open函数
- 使用python获取实时卫星云图
- C和Python实现冒泡法排序
- Python request第三方库的安装
- Thrift 連線至 Hbase 使用Python - ImportError: No module named Thrift
- python 爬虫1 开始,先拿新浪微博开始
- [Python] Python中的一些特殊函数
- Python 字典的使用
- Python 字典的使用
- python脚本自动保存blog页面
- python对文件的基本操作