我们经常会看到一个文章下面有很多的评论,特别的一些带有话题的文章下面,一般的评论数目几千或者几万都是很正常的。那么,我们有没有什么方法可以把众多的评论给提取出来呢?今天小编要给大家介绍json库的用法,之前小编偶尔也会提到过,不会用的不是很多,今天我们就提取评论的用法细细体会一下。
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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)
下一篇: 没有了
一级建造师二级建造师消防工程师造价工程师土建职称房地产经纪人公路检测工程师建筑八大员注册建筑师二级造价师监理工程师咨询工程师房地产估价师 城乡规划师结构工程师岩土工程师安全工程师设备监理师环境影响评价土地登记代理公路造价师公路监理师化工工程师暖通工程师给排水工程师计量工程师
执业药师执业医师卫生资格考试卫生高级职称护士资格证初级护师主管护师住院医师临床执业医师临床助理医师中医执业医师中医助理医师中西医医师中西医助理口腔执业医师口腔助理医师公共卫生医师公卫助理医师实践技能内科主治医师外科主治医师中医内科主治儿科主治医师妇产科医师西药士/师中药士/师临床检验技师临床医学理论中医理论