修复Python中破碎的HTML – Beautifulsoup无法正常工作
发布时间:2020-12-20 11:37:12 所属栏目:Python 来源:网络整理
导读:我有兴趣从这张表中删除文本: https://ows.doleta.gov/unemploy/trigger/2011/trig_100211.html 以及其他喜欢它的人. 我写了一个快速python脚本,适用于以类似方式格式化的其他表: state = "" weeks = "" edate = "" pdate = url[-11:] pdate = pdate[:-5]
我有兴趣从这张表中删除文本:
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,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> </tr> #theoretically,end of row about State1 因为一半的行没有正确格式化,所以BeautifulSoup忽略了它们.我已经尝试用整洁来修复问题,但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 docs:Differences Between Parsers.由于这个表在浏览器中呈现时看起来没问题,并且html5lib尝试以与浏览器相同的方式解析页面,所以这可能是一个安全的选择,这就是你想要的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |