直接贴代码叭,只要申请了个人百度账号并且改代码内对应的参数就可以直接实现
# -*- coding: utf-8 -*-
import json
import requests
import pandas as pd
import time
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
def get_sentiment_result(text):
"""
利用情感倾向分析API来获取返回数据
:param text: 输入文本
:return response: 返回的响应
"""
if text == '':
return ''
# 请求接口
url = "https://aip.baidubce.com/oauth/2.0/token"
# 需要先获取一个 token
client_id = 'client 账号'
client_secret = 'client 密码'
params = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret
}
headers = {
'Content-Type': 'application/json; charset=UTF-8'}
response = requests.post(url=url