如何在Python BeautifulSoup中找到带有特殊字符的XML标签

0 投票
1 回答
525 浏览
提问于 2025-04-30 08:43

我正在使用Python的BeautifulSoup库,版本是3。我的XML文件大概长这样(这是从docx格式来的):-

<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>

我想从'w:t'这个标签中提取内容,所以我做了以下操作:-

print soup.findAll('w:t')

这是我收到的错误信息:-

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

1 个回答

0

这个漂亮的对象必须这样定义:

BeautifulSoup(markup, "lxml-xml") 

或者

BeautifulSoup(markup, "xml")

就像在文档中说明的那样。

撰写回答