我使用python使用requests.put()方法将字符串上传到网站,但是当我试图将该字符串上传到网站时,我得到了一个错误。该错误以解析错误的形式出现,该错误声明:
Error parsing xhtml: Unexpected character ' ' (code 32) in content after '<' (malformed start element)我理解它为什么会显示,因为我的字符串中有“<”,而且这是html代码,解析器在不知道这是字符串的一部分的情况下寻找相应的code '>‘>。我试着用反斜杠('<')和('\<')来转义,但是它不起作用,因为我得到了一个语法错误,它声明转义字符的使用不正确。
import requests
import json
from requests.auth import HTTPBasicAuth
example_string = '<p><strong>Into managed branch from 2021-3-1 to 2021-5-3</strong></p><table><colgroup><col style=\\"width: 115.0px;\\" /><col style=\\"width: 95.0px;\\" /><col style=\\"width: 58.0px;\\" /><col style=\\"width: 105.0px;\\" /><col style=\\"width: 110.0px;\\" /><col style=\\"width: 215.0px;\\" /><col style=\\"width: 215.0px;\\" /></colgroup><tbody><tr><td><p>This is going < to print</p></td></tr></tbody></table><p class=\\"auto-cursor-target\\"><br /></p>'
headers = {
'Content-Type': 'application/json',
}
data = '{"id":"534756378","type":"page", "title":"GL_Engine Output","space":{"key":"CSSFW"},"body":{"storage":{"value":"' + example_string + '","representation":"storage"}}, "version":{"number":109}}'
json.loads(data)
print("JSON loads successful")
response = requests.put('https://confluence.ai.com/rest/api/content/534756378', headers=headers, data=data, auth=HTTPBasicAuth('svc-Auto@ai.com', 'AIengineering1@ai'))
print(response)
print(response.json())注意,'<‘在我的字符串中,“它将沿着example_string进一步打印”。
发布于 2021-05-16 08:24:57
https://stackoverflow.com/questions/67524007
复制相似问题