我正在使用BeautifulSoup解析多個XML文檔源,並且想要執行一些預處理以用自定義XML標記替換非標準CDATA
標記。爲了說明:用BeautifulSoup中的標籤替換CDATA NavigableStrings
下面的XML源...
<title>The end of the world as we know it</title>
<category><![CDATA[Planking Dancing]]></category>
<pubDate><![CDATA[Sun, 16 Sep 2012 12:00:00 EDT]]></pubDate>
<dc:creator><![CDATA[Bart Simpson]]></dc:creator>
...會變成:
<title>The end of the world as we know it</title>
<category><myTag>Planking Dancing<myTag></category>
<pubDate><myTag>Sun, 16 Sep 2012 12:00:00 EDT<myTag></pubDate>
<dc:creator><myTag>Bart Simpson<myTag></dc:creator>
我不認爲這個問題已經被問之前,SO(我嘗試了幾個不同的SO查詢)。我也嘗試了幾種不同的方法,使用.findAll('cdata', text=True)
並將BeautifulSoup replaceWith()
方法應用於每個產生的NavigableString
。我所做的嘗試導致沒有替換,或者看起來像遞歸循環。
我很高興能發佈我以前的嘗試,但考慮到這裏的問題是很簡單的,我希望有人可以張貼的如何完成一個明顯的例子,搜索和替換上述使用BeautifulSoup 3.