智能春联

# encoding:GBK

#原创禁止抄袭
import requests,json
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/couplets"+'?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)
    fh=json.loads(response2.text)
    print("上联",fh['couplets']['first'])
    print("下联",fh['couplets']['second'])
    print("横批",fh['couplets']['center'])    
except:
    shi=json.loads(response2.text)
    wrong=shi['error_msg']
    print(wrong)

 

 

你可能感兴趣的:(Python,爬虫,AI,python,json)