智能写诗参数错误
狮子LINQI 发布于2019-01-25 浏览:1239 回复:4
0
收藏
import requests
import json


url = 'http://aip.baidubce.com/rpc/2.0/nlp/v1/poem'
url=requests.get(url,params="25.359ff2c67791656ba5e93383d143b210.315360000.1863743605.282335-15412345").url   #这里最后五位为保证不被盗号改动
body = { 
"text": "李白",
"index": 0
}
re = requests.post(url,data=body,headers={'Content-Type': 'application/json'})
re = re.text
re = json.loads(re)
print(re)

这里print出来的是

{'error_code': 100, 'error_msg': 'Invalid parameter'}

本人也是小白,所以可能有一些比较离谱的错误

收藏
点赞
0
个赞
共4条回复 最后由hoker_ffb回复于2019-04-04
#5hoker_ffb回复于2019-04-04

后来解决了吗?

0
#4狮子LINQI回复于2019-01-25

已解决,原因是url没写tokenkey?

0
#3busyboxs回复于2019-01-25
# -*- coding:utf-8 -*-
import urllib
from urllib.request import urlopen, Request
from urllib.parse import urlencode
import sys
import ssl
import json
from pprint import pprint


def get_token_key():
    token_key = ''
    client_id = '自己填'
    client_secret = '自己填'

    host = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials' \
        f'&client_id={client_id}&client_secret={client_secret}'

    request = Request(host)
    request.add_header('Content-Type', 'application/json; charset=UTF-8')
    response = urlopen(request)
    token_content = response.read()
    if token_content:
        token_info = json.loads(token_content)
        token_key = token_info['access_token']
    return token_key


def nlp_result(text, token_key, index=0, way='poem'):
    assert way in ['poem', 'couplets'], 'type should be poem or couplet'
    request_url = f'https://aip.baidubce.com/rpc/2.0/nlp/v1/{way}'

    params_d = dict()
    params_d['text'] = text
    params_d['index'] = index
    params = json.dumps(params_d).encode('utf-8')
    access_token = token_key
    request_url = request_url + "?access_token=" + access_token
    request = Request(url=request_url, data=params)
    request.add_header('Content-Type', 'application/json')
    response = urlopen(request)
    content = response.read()
    if content:
        data = json.loads(content)
        if way == 'couplets':
            center = data['couplets']['center']
            first = data['couplets']['first']
            second = data['couplets']['second']
            return center, first, second
        else:
            title = data['poem'][0]['title']
            poem = data['poem'][0]['content'].replace('\t', '\n')
            return title, poem


# token_key = '自己填'  # get_token_key()
# print(token_key)
# center, first, second = nlp_result('春节', token_key, way='couplets')
# title, poem = nlp_result('春节', token_key, way='poem')

 

这是我用python3写的,可以参考参考

0
#2狮子LINQI回复于2019-01-25

加上verify=False一样不行

import requests
import json


url = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/poem'
url=requests.get(url,params="25.359ff2c67791656ba5e93383d143b210.315360000.1863743605.282335-15483853").url
body = { 
"text": "李白",
"index": 0
}
re = requests.post(url,verify=False,data=body,headers={'Content-Type': 'application/json'})
re = re.text
re = json.loads(re)
print(re)
0
TOP
切换版块