python - 在 Python 中修复损坏的 HTML - Beautifulsoup 不工作

标签 python web-scraping beautifulsoup html-table tidy

我有兴趣从此表中抓取文本: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 会忽略它们。我试过用 tidy 解决这个问题,但是 BeautifulSoup 在阅读它建议的代码时遇到了问题。我考虑过通过在正确的位置生成带有标签的新字符串来解决问题,但我不确定该怎么做。

有什么建议吗?

最佳答案

由于不同的解析器可以自由地处理它们认为合适的损坏的 HTML,因此在这些情况下,在尝试自行修复之前探索它们是如何处理的通常很有用。

在这种情况下,您可能会对如何 html5lib 感兴趣处理这个 - 在我看来它插入了丢失的 <tr>元素而不是丢弃所有孤立的 <td>lxml 这样的元素(默认值)。

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 文档中的更多信息:Differences Between Parsers .由于此表在浏览器中呈现时显示正常,并且 html5lib尝试以与浏览器相同的方式解析页面,可以肯定的是,这就是您想要的。

关于python - 在 Python 中修复损坏的 HTML - Beautifulsoup 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25269145/

相关文章:

python - 使用 Scippy 的 ndimage.map_coordinates 进行插值时出现意外结果

python - Django 的 TinyMCE

python - 如何使用 BeautifulSoup 抓取 Instagram

python - 无论如何要抓取重定向的链接?

web-scraping - UserWarning : The soupsieve package is not installed. 无法使用 CSS 选择器

python - 如何列出属于一组范围内的所有数字对?

python - Scrapy Contracts - 延迟中未处理的错误

excel - 使用查询选择器从 VBA 中抓取

python - 尝试抓取 HTML 表并转换为 Python 中的数据框。代码无法正常工作

当变量超出范围时,Python 不会释放内存