-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxiaoMi.py
63 lines (53 loc) · 1.91 KB
/
xiaoMi.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""小米评论抽取"""
import pymongo
import json
client = pymongo.MongoClient("localhost", 27017)
db = client.taobao
collectionItem = db.item
# print(collectionItem.count_documents({})) # 一共 19069 条商品记录
documents = collectionItem.find({
"笔记本型号": {"$regex": '^小米'},
'评论数': {'$ne': '暂无评论'},
"评论详情": {"$exists": True},
})
errInfo, userComments = [], []
for document in documents:
commentDetails = document['评论详情']
i = 0 # 用来查看报错时的条目索引
for ele in commentDetails:
details = []
keys = []
rateContentList = []
try:
if 'rateDetail' in ele:
keys = ['rateContent', 'rateDate']
details = ele['rateDetail']['rateList']
elif 'comments' in ele:
keys = ['content', 'date']
details = ele['comments']
else:
errInfo.append(document['产品ID'] + ' ' + str(i))
uselessInfo = [
'此用户没有填写评论!',
'此用户没有填写评价。',
'系统默认评论',
'15天内买家未作出评价',
'评价方未及时做出评价,系统默认好评!',
]
for comment in details:
if comment[keys[0]] in uselessInfo:
continue
rateContentList.append(comment[keys[0]])
except TypeError:
errInfo.append(document['产品ID'] + ' ' + str(i))
userComments.extend(rateContentList)
i += 1
# print(len(userComments)) # 13684
fileNames = [
'json/xiaoMiComment.json',
'json/errInfo.json',
]
fileContents = [userComments, errInfo]
for i in range(len(fileNames)):
with open(fileNames[i], 'w', encoding='utf-8') as f_obj:
json.dump(fileContents[i], f_obj, indent=4, ensure_ascii=False)