【问题标题】:Beautiful Soup - how to fix broken tagsBeautiful Soup - 如何修复损坏的标签
【发布时间】:2011-11-22 10:52:35
【问题描述】:

我想知道如何在用 Beautiful Soup 解析之前修复损坏的 html 标签。

在以下脚本中,td> 需要替换为 <td

如何进行替换以便 Beautiful Soup 可以看到?

from BeautifulSoup import BeautifulSoup

s = """
<tr>
td>LABEL1</td><td>INPUT1</td>
</tr>
<tr>
<td>LABEL2</td><td>INPUT2</td>
</tr>"""

a = BeautifulSoup(s)

left = []
right = []

for tr in a.findAll('tr'):
    l, r = tr.findAll('td')
    left.extend(l.findAll(text=True))
    right.extend(r.findAll(text=True))

print left + right

【问题讨论】:

    标签: python regex beautifulsoup


    【解决方案1】:

    编辑(工作):

    我从 w3 中获取了一个完整的(至少应该是完整的)列表来匹配的所有 html 标签。试试看:

    fixedString = re.sub(">\s*(\!--|\!DOCTYPE|\
                               a|abbr|acronym|address|applet|area|\
                               b|base|basefont|bdo|big|blockquote|body|br|button|\
                               caption|center|cite|code|col|colgroup|\
                               dd|del|dfn|dir|div|dl|dt|\
                               em|\
                               fieldset|font|form|frame|frameset|\
                               head|h1|h2|h3|h4|h5|h6|hr|html|\
                               i|iframe|img|input|ins|\
                               kbd|\
                               label|legend|li|link|\
                               map|menu|meta|\
                               noframes|noscript|\
                               object|ol|optgroup|option|\
                               p|param|pre|\
                               q|\
                               s|samp|script|select|small|span|strike|strong|style|sub|sup|\
                               table|tbody|td|textarea|tfoot|th|thead|title|tr|tt|\
                               u|ul|\
                               var)>", "><\g<1>>", s)
    bs = BeautifulSoup(fixedString)
    

    生产:

    >>> print s
    
    <tr>
    td>LABEL1</td><td>INPUT1</td>
    </tr>
    <tr>
    <td>LABEL2</td><td>INPUT2</td>
    </tr>
    
    >>> print re.sub(">\s*(\!--|\!DOCTYPE|\
                           a|abbr|acronym|address|applet|area|\
                           b|base|basefont|bdo|big|blockquote|body|br|button|\
                           caption|center|cite|code|col|colgroup|\
                           dd|del|dfn|dir|div|dl|dt|\
                           em|\
                           fieldset|font|form|frame|frameset|\
                           head|h1|h2|h3|h4|h5|h6|hr|html|\
                           i|iframe|img|input|ins|\
                           kbd|\
                           label|legend|li|link|\
                           map|menu|meta|\
                           noframes|noscript|\
                           object|ol|optgroup|option|\
                           p|param|pre|\
                           q|\
                           s|samp|script|select|small|span|strike|strong|style|sub|sup|\
                           table|tbody|td|textarea|tfoot|th|thead|title|tr|tt|\
                           u|ul|\
                           var)>", "><\g<1>>", s)
    
    <tr><td>LABEL1</td><td>INPUT1</td>
    </tr>
    <tr>
    <td>LABEL2</td><td>INPUT2</td>
    </tr>
    

    这个也应该匹配损坏的结束标签(&lt;/endtag&gt;):

    re.sub(">\s*(/?)(\!--|\!DOCTYPE|\a|abbr|acronym|address|applet|area|\
                     b|base|basefont|bdo|big|blockquote|body|br|button|\
                     caption|center|cite|code|col|colgroup|\
                     dd|del|dfn|dir|div|dl|dt|\
                     em|\
                     fieldset|font|form|frame|frameset|\
                     head|h1|h2|h3|h4|h5|h6|hr|html|\
                     i|iframe|img|input|ins|\
                     kbd|\
                     label|legend|li|link|\
                     map|menu|meta|\
                     noframes|noscript|\
                     object|ol|optgroup|option|\
                     p|param|pre|\
                     q|\
                     s|samp|script|select|small|span|strike|strong|style|sub|sup|\
                     table|tbody|td|textarea|tfoot|th|thead|title|tr|tt|\
                     u|ul|\
                     var)>", "><\g<1>\g<2>>", s)
    

    【讨论】:

    • 不走运。输出:
    • @terra 修正了正则表达式并编辑了我的答案,试试我现在在那里的 re.sub。我测试了它,它应该工作得很好。它检查所有 html 标签的完整列表(从 w3 中提取)。
    【解决方案2】:

    如果这是您唯一关心的 td> -> ,请尝试:

    myString = re.sub('td>', '<td>', myString)
    

    在将 myString 发送到 BeautifulSoup 之前。如果还有其他损坏的标签,请给我们一些示例,我们会处理它:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      相关资源
      最近更新 更多