python爬虫初体验(百度贴吧)
来源:互联网 发布:风力预报软件 编辑:程序博客网 时间:2024/06/05 22:43
最近闲来无事在网上看了会python的教程,发现上手比较简单。于是心血来潮想写个脚本玩玩。写什么呢?想起以前在贴吧里追的某个帖子,在线看起来特别麻烦,所以想把它爬下来看。
直接上代码:
#coding = utf-8import urllibimport sysfrom pyquery import PyQueryimport rereload(sys)sys.setdefaultencoding("utf-8")def getHtml(url): page = urllib.urlopen(url) html = page.read() return htmlhtml = unicode(getHtml("http://tieba.baidu.com/p/1894972828?see_lz=1"), "utf-8")q = PyQuery(html)href = q.find('.l_posts_num:first a:last').attr("href")reg = r'pn=(\d+)'result = re.search(reg, href)pageMax = result.group(1)i = 1file = open('content.txt', 'ab+')while i <= int(pageMax): html = unicode(getHtml("http://tieba.baidu.com/p/1894972828?see_lz=1&pn="+str(i)), "utf-8") q = PyQuery(html) content = q.find('cc').find('.j_d_post_content').text() file.write(content) print '第'+str(i)+'页完成' i += 1file.close()其中的pyquery默认是未安装的,需要使用easy_install安装。(easy_install是python下的一个包管理器,相当于nodejs的npm,如果没有安装请自行百度哈)
代码中因为涉及到中文,所以得加上
reload(sys)sys.setdefaultencoding("utf-8")不然就会报错。
0 0
- python爬虫初体验(百度贴吧)
- python百度贴吧爬虫
- python 百度贴吧爬虫
- python- 百度贴吧爬虫
- [python]百度贴吧爬虫
- python 百度贴吧爬虫(下载图片)
- 抓取百度贴吧python小爬虫 (2015最新版)
- Python爬虫实战(2):百度贴吧帖子
- Python爬虫实战(2):百度贴吧帖子
- python爬虫入门 实战(二)---爬百度贴吧
- Python 爬虫获取百度贴吧图片
- python爬虫百度贴吧标题数据
- python实现百度贴吧爬虫
- python爬虫实战2-百度贴吧
- Python爬虫学习(1):百度贴吧
- python爬虫--抓取百度贴吧
- Python爬虫学习2--百度贴吧
- 学习记录:python百度贴吧爬虫
- 程序猿读书
- jq动态生成二维码
- UML学习笔记动态图之状态图和活动图
- 学习嵌入式Linux-JZ2440-启动过程分析
- [OpenJudge-NOI]幂的末尾 快速幂
- python爬虫初体验(百度贴吧)
- matlab画图函数用法,subplot,plot
- UVa 12704 - Little Masters
- UML学习笔记之构件图和部署图
- 让linux Sort 按照 ASCII 顺序排序
- oracle分析函数技术详解(配上开窗函数over())----
- 如何在java Web项目中开发WebService接口
- codeforces-414B-Mashmokh and ACM
- Notepad++ 运行java