为了账号安全,请及时绑定邮箱和手机立即绑定

这个 XML 可以被解析吗?

这个 XML 可以被解析吗?

慕田峪7331174 2024-01-27 16:29:01
我正在尝试解析 XML 响应,但没有成功。我正在使用 python requests 库连接到返回 XML 的 API。如果我将其直接传递给 ElementTree :ElementTree.fromstring(response.content)它返回:xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0我从一开始就删除了:{“GetQuestions”:我从末尾删除:“}仍然返回xml.etree.ElementTree.ParseError。我的方法有问题还是 XML 有问题?任何意见,将不胜感激。
查看完整描述

3 回答

?
慕容708150

TA贡献1831条经验 获得超4个赞

您可以解析 xml,但之前需要进行一些小的清理。


见下文


import xml.etree.ElementTree as ET

import requests 

# response is a dict with 1 entry

response = requests.get('api_url_goes_here').json()

# TODO - remove next line when you actually call the API

response = {"GetQuestions":"<Questions><Question><QuestionId>393938<\/QuestionId><QuestionText>Please respond to the following statement:\"The assigned task was easy to complete\"<\/QuestionText><QuestionType>single<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393939<\/QuestionId><QuestionText>Did you save your  datafor later? Why\/why not?<\/QuestionText><QuestionType>text<\/QuestionType><QuestionStatus>1<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393940<\/QuestionId><QuestionText>Did you notice how much it cost to find the item? How much was it?<\/QuestionText><QuestionType>text<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393941<\/QuestionId><QuestionText>Did you select ‘signature on form’? Why\/why not?<\/QuestionText><QuestionType>text<\/QuestionType><QuestionStatus>1<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393942<\/QuestionId><QuestionText>Was it easy to find thethe new page? Why\/why not?<\/QuestionText><QuestionType>single<\/QuestionType><QuestionStatus>1<\/QuestionStatus><ExtendedType>4<\/ExtendedType><\/Question><Question><QuestionId>393943<\/QuestionId><QuestionText>Please enter your email. So that we can track your responses, we need you to provide this for each task.<\/QuestionText><QuestionShortCode>email<\/QuestionShortCode><QuestionType>text<\/QuestionType><QuestionStatus>1<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393944<\/QuestionId><QuestionText>Why didn't you save your  datafor later?<\/QuestionText><QuestionType>text<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393945<\/QuestionId><QuestionText>Why did you save your  datafor later?<\/QuestionText><QuestionType>single<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>4<\/ExtendedType><\/Question><Question><QuestionId>393946<\/QuestionId><QuestionText>Did you save your  datafor later?<\/QuestionText><QuestionType>single<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393947<\/QuestionId><QuestionText>Why didn't you select 'signature on form'?<\/QuestionText><QuestionType>text<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393948<\/QuestionId><QuestionText>Why did you select 'signature on form'?<\/QuestionText><QuestionType>text<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>4444449<\/QuestionId><QuestionText>Did you select ‘signature on form’?<\/QuestionText><QuestionType>single<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393950<\/QuestionId><QuestionText>Why wasn't it easy to find thethe new page?<\/QuestionText><QuestionType>single<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>4<\/ExtendedType><\/Question><Question><QuestionId>393951<\/QuestionId><QuestionText>Was it easy to find thethe new page?<\/QuestionText><QuestionType>single<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393952<\/QuestionId><QuestionText>Please enter your email addressSo that we can track your responses, we need you to provide this for each task<\/QuestionText><QuestionShortCode>email<\/QuestionShortCode><QuestionType>single<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>4<\/ExtendedType><\/Question><\/Questions>"}


# fetch the xml string and do a quick cleanup

xml = response['GetQuestions'].replace('<\/','</')

root = ET.fromstring(xml)

print(root)

输出


<Element 'Questions' at 0x7f35c68919f0>


查看完整回答
反对 回复 2024-01-27
?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

使用该 JSON 字符串response

xml.etree.ElementTree.fromstring(response.json()['GetQuestions'])


查看完整回答
反对 回复 2024-01-27
?
www说

TA贡献1775条经验 获得超8个赞

您可以使用BeautifulSoup来解析xml内容。当你创建一个变量时,你应该这样写:your_variable = BeautfilSoup(requests.text, features="xml" )。那应该对你有用。还要尝试验证您的代码,以便这一行成为完整的代码。一定有错误。然而,由于它是一行代码,因此很难找到位置。您可以访问验证器网站


查看完整回答
反对 回复 2024-01-27
  • 3 回答
  • 0 关注
  • 133 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信