how to find an xml tag with special character in Python BeautifulSoup

☆樱花仙子☆ 提交于 2019-12-25 14:14:19

问题


I am using Python BeautifulSoup version 3. my xml looks something like this (its from docx format):-

<w:r w:rsidRPr="00541D75">
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
<w:b/>
<w:color w:val="1F497D" w:themeColor="text2"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:t>Mandatory / Optional</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>

I wanted to extract out the content from tag 'w:t', and so this is what i did:-

print soup.findAll('w:t')

This is the error message that i got:-

print soup.findAll('w:t')
UnicodeEncodeError: 'ascii' codec can't encode character u'\u201c' in position 43: ordinal not in range(128)

回答1:


the beautiful object must be defined as follow :

BeautifulSoup(markup, "lxml-xml") 

or

BeautifulSoup(markup, "xml")

as in the doc specified.



来源:https://stackoverflow.com/questions/26626908/how-to-find-an-xml-tag-with-special-character-in-python-beautifulsoup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!