百度智能写诗

# encoding:utf-8

#请先去百度自然语言处理里面创建一个应用,开启智能写诗,然后记得AK和SK就行

#原创文章,禁止抄袭或转载
import requests,json,re
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=你的AK&client_secret=你的SK'
response = requests.get(host,timeout=20)
token=json.loads(response.text)
token=token['access_token']#提取token
url="https://aip.baidubce.com/rpc/2.0/creation/v1/poem"+'?access_token='+str(token)
headers={'Content-Type':'application/json'}
params={'text':'幸福','index':0}#这是你的诗词的主题
try:
    response2=requests.post(url,headers=headers,data=json.dumps(params),timeout=20)
    shi=json.loads(response2.text)
    shi=shi['poem'][0]['content']
    print(shi)
except:
    shi=json.loads(response2.text)
    wrong=shi['error_msg']#提取报错
    print(wrong)

 

 

你可能感兴趣的:(Python,AI,python,百度,ai)