request.post上的response.content坏了[重复]。

问题描述 投票:-1回答:1

使用python 3.7

我想建立一个小脚本,用api来发布数据,但是我无法正确得到response.content,因为有一些奇怪的字符出来。

import requests
files = {
    'content': (None, 'text'),
}
response = requests.post('http://localhost/api/v2', files=files)
print (response.content)

输出:b'http:/localhost1NSZAOWE。\n'

我试着擦掉字符做

txt = (response.content)
print (txt[1:])

产出:b'tp:/localhost2LSALWE/n'

谁能帮我找到一种方法来避免在url中得到那些奇怪的字符,并以response.content的形式出现?

python python-3.x python-requests
1个回答
1
投票

response.content 返回的是字节字符串,所以你必须把它转换为字符串(utf-8),然后使用 strip() 去掉尾行

response.content.decode('utf-8').strip()

核对 将字节转换为字符串

© www.soinside.com 2019 - 2024. All rights reserved.