如何在Python BeautifulSoup中找到带有特殊字符的xml标记 [英] how to find an xml tag with special character in Python BeautifulSoup

查看:293
本文介绍了如何在Python BeautifulSoup中找到带有特殊字符的xml标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python BeautifulSoup版本3. 我的xml看起来像这样(来自docx格式):-

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>

我想从标签"w:t"中提取内容,所以这就是我所做的:-

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)

推荐答案

漂亮的对象必须定义如下:

the beautiful object must be defined as follow :

BeautifulSoup(markup, "lxml-xml") 

BeautifulSoup(markup, "xml")

指定的文档中所述.

这篇关于如何在Python BeautifulSoup中找到带有特殊字符的xml标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆