ParseError: not well-formed (invalid token) using cElementTree

前端 未结 13 1041
日久生厌
日久生厌 2020-12-16 11:10

I receive xml strings from an external source that can contains unsanitized user contributed content.

The following xml string gave a ParseError in cElementTre

13条回答
  •  天涯浪人
    2020-12-16 11:29

    None of the above fixes worked for me. The only thing that worked was to use BeautifulSoup instead of ElementTree as follows:

    from bs4 import BeautifulSoup
    
    with open("data/myfile.xml") as fp:
        soup = BeautifulSoup(fp, 'xml')
    

    Then you can search the tree as:

    soup.find_all('mytag')
    

提交回复
热议问题