
我们经常会看到一个文章下面有很多的评论,特别的一些带有话题的文章下面,一般的评论数目几千或者几万都是很正常的。那么,我们有没有什么方法可以把众多的评论给提取出来呢?今天小编要给大家介绍json库的用法,之前小编偶尔也会提到过,不会用的不是很多,今天我们就提取评论的用法细细体会一下。
在网上看过移动端的微博数据很好爬,没有异步加载,可是不知道为什么,评论数据就是异步加载的,其实异步加载不可怕,找到相应js包即可,如下图为沈梦辰的一条微博和评论的js包。我们只需请求这个js数据,然后利用json库即可提取我们所需的评论数据。

代码
import requests
import json
import time
import pymongo
client = pymongo.MongoClient('localhost', 27017)
weibo = client['weibo']
comment_shengmengc = weibo['comment_shengmengc']
headers = {
"Cookies":'xxxxxxxxxxx',
"User-Agent":'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
}
url_comment = ['http://m.weibo.cn/api/comments/show?id=4060977869675098&page={}'.format(str(i)) for i in range(0,1000)]
def get_comment(url):
wb_data = requests.get(url,headers=headers).text
data_comment = json.loads(wb_data)
try:
datas = data_comment['data']
for data in datas:
comment = {"comment":data.get("text")}
comment_shengmengc.insert_one(comment)
except KeyError:
pass
for url in url_comment:
get_comment(url)
time.sleep(2)
通过以上的json的使用,我们就可以把大量的评论给提取出来了,相信很多小伙伴也好奇那些评论是什么,那就赶紧动手尝试一下吧。
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试



