赞
踩
- import requests
- from bs4 import BeautifulSoup
-
- res = requests.get('http://news.sina.com.cn/c/nd/2017-06-12/doc-ifyfzhac1650783.shtml')
- res.encoding = 'utf-8'
- soup = BeautifulSoup(res.text,'html.parser')
- #取评论数
- commentCount = soup.select_one('#commentCount1')
- print(commentCount.text)
因此我们需要取寻找存储评论内容的js 经过查找我们发现其存储在改js里
将相应内容放入json数据查看器中我们发现评论总数和评论内容都在该js文件中一json格式存放
在消息头中我们可以看的该js文件的访问路径及请求方式
代码示例
- import json
- comments = requests.get('http://comment5.news.sina.com.cn/page/info?version=1&format=js&channel=gn&newsid=comos-fyfzhac1650783')
- comments.encoding = 'utf-8'
- print(comments)
- jd = json.loads(comments.text.strip('var data=')) #移除改var data=将其变为json数据
- print(jd['result']['count']['total'])
取评论总数时为何使用jd['result']['count']['total']
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。