forked from 17fx/weibo-hotsearch-analyse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nlp.py
72 lines (52 loc) · 1.85 KB
/
nlp.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
64
65
66
67
68
69
70
71
72
import json,os
import types
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.nlp.v20190408 import nlp_client, models
#填入腾讯云密钥
SecretId = ''
SecretKey = ''
#打开raw目录下文件
def loadfile(path='raw/2021-03-17.json'):
f = open(path,'r+',encoding='utf8')
data = json.load(f)
f.close()
all_hot = [ title['title'] for title in data]
return all_hot
def nlp_api(params):
try:
cred = credential.Credential(SecretId,SecretKey)
httpProfile = HttpProfile()
httpProfile.endpoint = "nlp.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = nlp_client.NlpClient(cred, "", clientProfile)
req = models.ClassifyContentRequest()
req.from_json_string(json.dumps(params))
resp = client.ClassifyContent(req)
back = json.loads(resp.to_json_string())
except TencentCloudSDKException as err:
print(err)
return back
def check(path):
all_hot = loadfile(path)
outcome = {}
num = 1
for trending in all_hot:
print(num,'/',len(all_hot),end='\r')
num += 1
params = {
"Title": "微博热搜",
"Content": [trending]
}
back = nlp_api(params)
outcome[trending] = back
return outcome
if __name__ == '__main__':
path = r'raw\2024-04-27.json'
data = json.dumps(check(path),ensure_ascii=False)
path_name = path.split('\\')[1]
with open('output'+path_name,'a+',encoding='utf8') as f:
f.write(data)