簡體   English   中英

使用Beautiful Soup進行XML解析的問題

[英]Problem with XML-parsing using Beautiful Soup

當嘗試用Beautiful Soup替換XML中的某些元素時,我發現必須使用soup.find_all().string.replace_with()替換所需的元素。 但是,我遇到了一個問題,即soup.find_all()方法僅返回類型為None元素。

因此,我嘗試將問題分解為盡可能基本的XML:

from bs4 import BeautifulSoup as BS

xml = """
<xml>
    <test tag="0"/>
    <test tag="1"/>
</xml>"""

soup = BS(xml, 'xml')
for elem in soup.find_all("test"):
    print('Element {} has type {}.'.format(elem, elem.type))

這給出了完全相同的東西:

Element <test tag="0"/> has type None.
Element <test tag="1"/> has type None.

如果有人指出問題出在哪里,我會很高興。

提前致謝

好吧,我不確定您要查找的輸出內容是什么,但是您可以通過以下方式替換標記屬性:

from bs4 import BeautifulSoup as BS

xml = """
<xml>
    <test tag="0"/>
    <test tag="1"/>
</xml>"""

replace_list = ['0']
replacement = '2'

soup = BS(xml, 'xml')
for elem in soup.find_all("test"):
    if elem['tag'] in replace_list:
        elem['tag'] = replacement
    #print('Element {} has type {}.'.format(elem, elem.name))

xml = str(soup)

print (xml)

輸出:

<?xml version="1.0" encoding="utf-8"?>
<xml>
<test tag="2"/>
<test tag="1"/>
</xml>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM