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