使用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的形式出现?
response.content
返回的是字节字符串,所以你必须把它转换为字符串(utf-8),然后使用 strip()
去掉尾行
response.content.decode('utf-8').strip()
核对 将字节转换为字符串