修复Python中的坏HTML - Beautifulsoup失效

3 投票
1 回答
2890 浏览
提问于 2025-04-18 17:01

我想从这个表格中抓取文本:https://ows.doleta.gov/unemploy/trigger/2011/trig_100211.html,还有其他类似的表格。

我写了一个简单的Python脚本,可以处理其他格式相似的表格:

    state = ""
    weeks = ""
    edate = "" 
    pdate = url[-11:]
    pdate = pdate[:-5]

    table = soup.find("table") 

    for row in table.findAll('tr'):     
        cells = row.findAll("td")
        if len(cells) == 13: 
            state = row.find("th").find(text=True) 
            weeks = cells[11].find(text=True) 
            edate = cells[12].find(text=True)
            try:   
                print pdate, state, weeks, edate 
                f.writerow([pdate, state, weeks, edate])
            except:  
                print state[1] + " error"  

但是,这个脚本在这个表格上不管用,因为有一半的行的标签格式不正确。也就是说,有一半的行没有用标签来标记行的开始:

</tr> #end of last row, on State0  
<td headers = "State1 no info", attributes> <FONT attributes> text </FONT> </td>
<td headers = "State1 no info", attributes> <FONT attributes> text </FONT> </td>
<td headers = "State1 no info", attributes> <FONT attributes> text </FONT> </td>
<td headers = "State1 no info", attributes> <FONT attributes> text </FONT> </td>
</tr> #theoretically, end of row about State1 

由于一半的行没有正确格式化,BeautifulSoup(一个用来解析HTML的库)就忽略了它们。我试过用tidy工具来修复这个问题,但BeautifulSoup在读取它建议的代码时也遇到了问题。我考虑过通过生成一个新的字符串,把标签放在正确的位置来解决这个问题,但我不太确定该怎么做。

有什么建议吗?

1 个回答

9

因为不同的解析器可以根据自己的方式处理有问题的HTML,所以在尝试自己修复之前,了解它们是怎么处理的通常是很有用的。

在这种情况下,你可能会对html5lib的处理方式感兴趣——我觉得它会插入缺失的<tr>元素,而不是像lxml(默认解析器)那样丢弃所有孤立的<td>元素。

soup = BeautifulSoup(text) #default parser - lxml

soup.table.find_all('tr')[9]
Out[31]: 
<tr bgcolor="#C0C0C0">
<td align="center" headers="Arizona noinfo" width="25"><font size="-2"> </font></td>
<td align="center" headers="Arizona noinfo" width="25"><font size="-2"> </font></td>
<td align="center" headers="Arizona noinfo" width="25"><font size="-2"> </font></td>
<th align="left" id="Arizona " width="100"><font size="-2">Arizona </font></th>
<td align="center" headers="Arizona noinfo" width="50"><font size="-2">2</font></td>
<td align="center" headers="Arizona noinfo" width="50"><font size="-2">2</font></td>
<td align="center" headers="Arizona 13_week_IUR indicators" width="50"><font size="-2">3.03</font></td>
<td align="center" headers="Arizona pct_of_prior_2years indicators" width="50"><font size="-2">79</font></td>
<td align="center" headers="Arizona 3_mo_satur indicators" width="50"><font size="-2">9.3</font></td>
<td align="center" headers="Arizona year pct_of_prior indicators" width="50"><font size="-2">94</font></td>
<td align="center" headers="Arizona 2nd_year pct_of_prior indicators" width="50"><font size="-2">93</font></td>
<td align="center" headers="Arizona 2nd_year pct_of_prior indicators" width="50"><font size="-2">155</font></td>
<td align="center" headers="Arizona avail_wks pct_of_prior indicators noinfo" width="50"><font size="-2"> </font></td>
<td align="center" headers="Arizona dates periods status" width="100"><font size="-2">E 06-11-2011</font></td>
</tr>

soup = BeautifulSoup(text, 'html5lib')

soup.table.find_all('tr')[9] #same path, different result!
Out[33]: 
<tr><td align="center" headers="Alaska noinfo" width="25"><font size="-2"> </font></td>
<td align="center" headers="Alaska noinfo" width="25"><font size="-2"> </font></td>
<td align="center" headers="Alaska noinfo" width="25"><font size="-2"> </font></td>
<th align="left" id="Alaska " width="100"><font size="-2">Alaska </font></th>
<td align="center" headers="Alaska noinfo" width="50"><font size="-2">2</font></td>
<td align="center" headers="Alaska noinfo" width="50"><font size="-2">2</font></td>
<td align="center" headers="Alaska 13_week_IUR indicators" width="50"><font size="-2">3.82</font></td>
<td align="center" headers="Alaska pct_of_prior_2years indicators" width="50"><font size="-2">90</font></td>
<td align="center" headers="Alaska 3_mo_satur indicators" width="50"><font size="-2">7.6</font></td>
<td align="center" headers="Alaska year pct_of_prior indicators" width="50"><font size="-2">96</font></td>
<td align="center" headers="Alaska 2nd_year pct_of_prior indicators" width="50"><font size="-2">95</font></td>
<td align="center" headers="Alaska 2nd_year pct_of_prior indicators" width="50"><font size="-2">117</font></td>
<td align="center" headers="Alaska avail_wks pct_of_prior indicators noinfo" width="50"><font size="-2"> </font></td>
<td align="center" headers="Alaska dates periods status" width="100"><font size="-2">E 06-11-2011</font></td>
</tr>

更多信息请查看bs4文档:解析器之间的区别。由于这个表格在浏览器中显示正常,而html5lib试图以浏览器的方式解析页面,所以可以认为这可能是你想要的处理方式。

撰写回答