网站制作学习网Python→正文:Body ('') is not valid Latin-1
字体:

Body ('') is not valid Latin-1

Python 2022/8/1 21:01:31  点击:不统计

<网f站o学a习s制p作.cn>

在做python post 提交接口数据时,遇到了中文错误的提示 Body ('中文内容') is not valid Latin-1

我的代码如下:

def post_api_get_book_chapters_result(self, article_id):

    url_data = "article_name=" + str('中文名称') + "&api_key=" + str(self.api_key)

    headers = {'Content-Type': 'application/x-www-form-urlencoded'}

    req = requests.post(self.api_url, data=url_data, headers=headers)

    data = json.loads(req.text)

    return data

    pass

提示错误

Body ('') is not valid Latin-1 ,查询网络上编码问题,后来修改了一下通过 encode()方法处理对应数据

修改 

def post_api_get_book_chapters_result(self, article_id):

    url_data = "article_name=" + str('中文名称') + "&api_key=" + str(self.api_key)

    headers = {'Content-Type': 'application/x-www-form-urlencoded'}

    req = requests.post(self.api_url, data=url_data.encode(), headers=headers)

    data = json.loads(req.text)

    return data

    pass

 

这样,就可以提交数据了~


%77w%77%2E%66%6F%72%61%73%70%2E%63%6E

·上一篇:Python将秒转换成分钟 >>    ·下一篇:python 获取昨天日期 >>
推荐文章
最新文章