简单的python爬虫——贴吧上取邮箱
来源:互联网 发布:糠秕孢子菌毛囊炎 知乎 编辑:程序博客网 时间:2024/06/07 07:03
这是一个比较简单的爬虫,只用到了两个简单的库re和urllib,
程序使用的是python2.7
urllib模块是用来获取原文网页,
re模块是用来匹配特定的字符的,
1.获取链接的最后一页
html = urllib.urlopen(url).read()reyuan = r'<a href=".*?pn=(.*?)">尾页</a>'recom = re.compile(reyuan)refind = re.findall(recom,html)
注意事项:设置编码类型为utf-8,如果定义gb2312,不能获取到网页的尾页,这是字符编码的问题,python3中就没有这样的问题
2.逐页遍历,获得邮箱
a = 尾页数#由上面的代码得到while i<=int(a): content = urllib.urlopen(url+str(i)).read() print("现在在下载第"+str(i)+"页,总共"+str(a) +"页") i += 1 pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,5}' items =re.findall(pattern,content) for item in items: print item
注意事项:如果按照上面输出的是乱码可以这么写
print("现在在下载第".decode("utf-8").encode("gb2312")+str(i)+"页,总共".decode("utf-8").encode("gb2312")+str(get_ye(url)) +"页".decode("utf-8").encode("gb2312")) i += 1
3.将邮箱保存到文件中
file = open("E:\\python\\qqcom1.txt","w+")file.write(item+ '\n')file.close()
注意事项:记得最后关闭文件
4.整理代码
#coding:utf-8import urllibimport refile = open("E:\\python\\qqcom1.txt","w+")url = "http://tieba.baidu.com/p/4194772383?pn="def get_ye(url): html = urllib.urlopen(url).read() reyuan = r'<a href=".*?pn=(.*?)">尾页</a>' recom = re.compile(reyuan) refind = re.findall(recom,html) return refind[0]def get_qq(): i = 1 j = 1 while i<=int(get_ye(url)): content = urllib.urlopen(url+str(i)).read() print("现在在下载第"+str(i)+"页,总共"+str(get_ye(url)) +"页") i += 1 pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,5}' items =re.findall(pattern,content) for item in items: file.write(item+ '\n') j += 1 else: print "结束" file.write(str(j)+ '\n') print j file.close()if __name__=="__main__": get_qq()
如果输出乱码就可以在每个字符串后面加上decode(“utf-8”).encode(“gb2312”)就可以了
( 写于2015年12月16日,http://blog.csdn.net/bzd_111)
0 0
- 简单的python爬虫——贴吧上取邮箱
- 简单的python爬虫——贴吧上取邮箱
- python爬虫——写出最简单的网页爬虫
- Python简单的爬虫
- 简单的python爬虫
- 简单的Python 爬虫
- 简单爬虫python实现02——BeautifulSoup的使用
- python初学者——一个简单的网络爬虫
- python——基于煎蛋网的简单图片爬虫
- Python爬虫——自制简单的搜索引擎
- python网络爬虫入门(一)——简单的博客爬虫
- python爬虫系列(1)——一个简单的爬虫实例
- python 爬虫试手,好简单的爬虫
- Python简单爬虫——淘宝数据
- python爬虫学习获取邮箱
- PYTHON 爬虫简单的认识
- python简单的爬虫代码
- [python]简单的网络爬虫
- fork产生子进程利用pipe管道通信
- android dialog 里面获取Activity的Context
- ECSHOP整合第三方登录,QQ登录、新浪微博登录、支付宝登
- 对WM_NCHITTEST消息的了解+代码实例进行演示
- jquery判断多个input输入框不能输入相同的值
- 简单的python爬虫——贴吧上取邮箱
- Java并发编程之ConcurrentHashMap
- tab切换导航内容制作
- vim 剪切(d) 复制(y=yank)
- Android UiAutomator环境配置
- mysql 设置字符集为utf8
- JS之typeof
- 通过.htaccess 对网站图片的基本防盗处理
- Hibernate Session