注册账号之后进行应用申请,有限制的次数,但是测试是肯定够用。
然后申请一个新的应用就可以了。
申请之后就能得到下面的 Api_Key 和 Secret_Key 进行接口测试。
当然需要先进行access_token的获取https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu,
获取之后才能进行后面的文本分析https://ai.baidu.com/ai-doc/NLP/zk6z52hds。
import requests
import time
import pandas as pd
import json
Api_Key = \'XXXXX\'
Secret_Key = \'XXXXX\'
def emotion(text):
headers = {\'User-Agent\':\'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)\'}
url = \'https://aip.baidubce.com/oauth/2.0/token\'
data = {
\'grant_type\':\'client_credentials\',
\'client_id\':Api_Key,
\'client_secret\':Secret_Key
}
response = requests.post(url=url, headers = headers, data=data)
dicts = json.loads(response.text)
print(dicts[\'access_token\'])
url = \'https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify?charset=UTF-8&access_token={}\'.format(dicts[\'access_token\'])
data = {
\'text\':text
}
data = json.dumps(data)
response = requests.post(url=url, headers = headers, data=data)
PD = json.loads(response.text)
print(PD)
print(PD[\'items\'][0][\'positive_prob\'])
print(PD[\'items\'][0][\'sentiment\'])
if __name__ == "__main__":
text = input()
emotion(text)