python 通过百度api 智能创作结构化文章,天气预报

首先创建好应用,便于后期获取token

在智能文章项目区,建立定制项目,建立后可获取项目ID
获取项目id

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值