Python ElementTree parsing unbound prefix error

前端 未结 2 1848
长发绾君心
长发绾君心 2021-01-07 19:01

I am learning ElementTree in python. Everything seems fine except when I try to parse the xml file with prefix:

test.xml:



        
2条回答
  •  粉色の甜心
    2021-01-07 19:20

    See if this works:

    from bs4 import BeautifulSoup
    
    xml_file = "test.xml"
    
    with open(xml_file, "r", encoding="utf8") as f:
        contents = f.read()
        soup = BeautifulSoup(contents, "xml")
    
        items = soup.find_all("country")
        print (items)
    

    The above will produce an array which you can then manipulate to achieve your aim (e.g. remove html tags etc.):

    [
    , 
    , 
    ]
    

提交回复
热议问题