python爬取网易评论
2015-12-09 20:08
621 查看
最近学习了json模块,并用它爬取了网易的评论。
爬取的是“最近华北空气污染严重”的新闻(http://comment.news.163.com/news3_bbs/B9MBB38O00014Q4P.html)
1、首先获取json格式的文件:我用的是360浏览器(貌似用谷歌比较好,但我谷歌出了点问题
所以用的360,不过没啥影响)
打开评论的网页,选取‘工具—开发人员工具’,或直接按F12,得到如下工具,点击Network,可以直接点 Scripts 得到 json 文件,一开始不知道评论是哪个文件。后来发现后缀名是 .html 的是评论网页。
2、网址:网易的评论有两种,一个是热门跟帖,一个是最新跟帖,两种帖子的网址不同,其中热门只有一个,而最新是多页的
爬取的是“最近华北空气污染严重”的新闻(http://comment.news.163.com/news3_bbs/B9MBB38O00014Q4P.html)
1、首先获取json格式的文件:我用的是360浏览器(貌似用谷歌比较好,但我谷歌出了点问题
所以用的360,不过没啥影响)
打开评论的网页,选取‘工具—开发人员工具’,或直接按F12,得到如下工具,点击Network,可以直接点 Scripts 得到 json 文件,一开始不知道评论是哪个文件。后来发现后缀名是 .html 的是评论网页。
2、网址:网易的评论有两种,一个是热门跟帖,一个是最新跟帖,两种帖子的网址不同,其中热门只有一个,而最新是多页的
#热门跟帖,也许它后面有个"_1",但若改成“_2”或更大时,它都长得一样但并不是评论! http://comment.news.163.com/data/news3_bbs/df/B9MBB38O00014Q4P_1.html #最新跟帖 http://comment.news.163.com/cache/newlist/news3_bbs/B9MBB38O00014Q4P_1.html</span>[/code] 所以要同时爬取两种网址。
3、处理字符串:用 json.loads() 解码字符串转换为python形式时格式很重要,在这里转换成字典形式。
将开头和结尾去掉,只剩下一个大字典,并去掉里面多余的信息
开头:
结尾:
这时候可以 printpost.keys() 试一下,可以得到字典中的键,在两种网址中,包含评论的键为:
接着获取这两个键里的值,可以得到每条帖子的信息:
接下来就可以获得评论了。
4、报错:socket.error: [Errno 10054],查了一下是远程主机重置了链接,是大量访问的原因,加了一个 time.sleep()就好了。
5、代码如下:#coding=utf-8 import urllib2 import json import re import time class WEPL: def __init__(self): self.user_agent = 'Mozilla/4.0(compatible;MSIE 5.5;Windows NT)' self.headers = {'User_Agent' :self.user_agent} self.url1='http://comment.news.163.com/data/news3_bbs/df/B9MBB38O00014Q4P_1.html' def getPageIndex(self,pageIndex): url2='http://comment.news.163.com/cache/newlist/news3_bbs/B9MBB38O00014Q4P_'+str(pageIndex)+'.html' return url2 def getHtml(self,url): try: request=urllib2.Request(url,headers=self.headers) response=urllib2.urlopen(request) html=response.read() return html except urllib2.URLError,e: if hasattr(e,'reason'): print u'loading error',e.reason return None def getPost(self): for i in range(1,10): #两种网址分开处理 if i==1: html=self.getHtml(self.url1) data1=re.sub('^var replyData=','',html) data2=data1[:-1] else: url2=self.getPageIndex(i) html=self.getHtml(url2) data1=re.sub('^var newPostList=','',html) data2=data1[:-2] data3=re.sub(" \[\<a href=''\>(.*?)\<\\\\/a\>\]: ","",data2) data4=re.sub("\<span (.*?)\<\\\\/span\>","",data3) data5=re.sub("\<br\>","",data4) #将json文件解码为python格式 post=json.loads(data5) if i==1: for allvalue in post['hotPosts']: with open('pl3.txt','a+') as fd: fd.write(allvalue['1']['f'].encode('utf-8')+'('+'IP:') fd.write(allvalue['1']['ip'].encode('utf-8')+')'+'\n'+'---') fd.write(allvalue['1']['b'].encode('utf-8')+'\n') else: for allvalue in post['newPosts']: with open('pl3.txt','a+') as fd: fd.write(allvalue['1']['f'].encode('utf-8')+'('+'IP:') fd.write(allvalue['1']['ip'].encode('utf-8')+')'+'\n'+'---') fd.write(allvalue['1']['b'].encode('utf-8')+'\n') #防止链接被重置 time.sleep(2) spider=WEPL() spider.getPost()
相关文章推荐
- python splinter chromedriver下载地址(国内可用)
- 初次感受python的力量
- python从控制台输入
- python- shutil 高级文件操作
- Python GUI--Tkinter实践
- python source compile and upgrade
- Python的POST和GET方法
- Python的POST和GET方法
- Python的POST和GET方法
- Python中的ftplib模块调用与测试
- PyPI中国镜像知多少和国内的python用户如何使用pypi镜像源
- Python 部署解析模板
- Python 多线程教程:并发与并行
- Beginning Python Chapter4
- python-50: 验证码
- pip装了一个包,但是python里Import的时候找不到怎么办?
- Python的Web开发环境(二)--Windows
- python基础教程共60课-第20课命令行常用命令
- 【详解】Python脚本转可执行文件
- mac 安装python软件包的工具--easy_install