亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

這個 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 關注
  • 241 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號