0
我遇到問題才能找到正確的解決方案。如果<answer>
= 99,我想要刪除<question>
及其子級。因此,我需要一個帶過濾問題的字符串。我有以下HTML結構:BeautifulSoup/LXML.html:如果孩子看起來像x,則刪除標記及其子項
<html>
<body>
<questionaire>
<question>
<questiontext>
Do I have a question?
</questiontext>
<answer>
99
</answer>
</question>
<question>
<questiontext>
Do I love HTML/XML parsing?
</questiontext>
<questalter>
<choice>
1 oh god yeah
</choice>
<choice>
2 that makes me feel good
</choice>
<choice>
3 oh hmm noo
</choice>
<choice>
4 totally
</choice>
</questalter>
<answer>
4
</answer>
</question>
<question>
</questionaire>
</body>
</html>
到目前爲止,我試着用XPath來實現它......但lxml.html沒有iterparse ......有嗎?感謝名單!
嗨馬特感謝你的答案......這看起來很複雜......我不知道是否有是BeautifulSoup還是lxml的解決方案...? – Jurudocs
我更新了我的答案,以便它可以與你的html一起工作。要警告你最後有一個'',這會導致解析錯誤。 –
非常感謝你......我發現minidom太可怕了,但這看起來不錯!我個人更喜歡lxml ...我希望我能接受兩個答案;-) – Jurudocs