如何在Python BeautifulSoup中找到具有特殊字符的xml标记

时间:2014-10-29 09:38:37

标签: python xml beautifulsoup

我正在使用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>

我想从标签中提取出内容&#39; w:t&#39;,这就是我所做的: -

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 :(得分:0)

美丽的物体必须定义如下:

Sub Test()

Dim firstname As String, lastname As String, fullname As Variant
Dim sht As Worksheet, lastrow As Long, lastrow2 As Long

Set sht = ThisWorkbook.Worksheets("Sheet1")
lastrow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
lastrow2 = sht.Cells(sht.Rows.Count, "F").End(xlUp).Row

For i = 2 To lastrow
    fullname = Split(Range("B" & i).Value, " ")
    firstname = fullname(0)
    lastname = fullname(1)
    For j = 2 To lastrow2
        If Range("A" & i).Value = Range("F" & j).Value Then
            If InStr(LCase(Range("G" & j).Value), LCase(firstname)) > 0 Then
                If InStr(LCase(Range("G" & j).Value), LCase(lastname)) > 0 Then
                    Range("H" & j).Value = Range("C" & i).Value
                End If
            End If
        End If
    Next j
Next i

End Sub

BeautifulSoup(markup, "lxml-xml") 

,如doc specified