import requests
def gettoken():
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【你的百度client id】&client_secret=【你的百度secret】'
response = requests.post(host)
if response:
json_res = response.json()
access_token = json_res['access_token']
print(json_res['access_token'])
return access_token
#结构化数据写作
def ai_artic(access_token):
host = 'https://aip.baidubce.com/rest/2.0/nlp/v1/gen_article?access_token=' + access_token
Headers = {
'Content-Type':'application/x-www-form-urlencoded'
}
data = {
'project_id':'你的项目id',
'city':'北京' #或者其他城市
}
response = requests.post(host,headers=Headers,data=data)
if response:
json_res = response.json()
contents = json_res['result']['texts'][0]
print(contents)
return contents
#调用区
access_token = gettoken()
ai_artic(access_token)