【发布时间】:2014-04-30 17:45:36
【问题描述】:
我正在尝试解析文档http://www.consilium.europa.eu/uedocs/cms_data/docs/pressdata/en/ecofin/acf8e.htm。 我想在文档的开头获取国家和名称。
这是我的代码
import urllib
import re
from bs4 import BeautifulSoup
url="http://www.consilium.europa.eu/uedocs/cms_data/docs/pressdata/en/ecofin/acf8e.htm"
soup=BeautifulSoup(urllib.urlopen(url))
attendances_table=soup.find("table", {"width":850})
print attendances_table #this works, I see the whole table
print attendances_table.find_all("tr")
我收到以下错误:
AttributeError: 'NoneType' object has no attribute 'next_element'
然后我尝试使用与这篇文章中相同的解决方案(我再次知道,我:p): beautifulsoup with an invalid html document
我换了行:
soup=BeautifulSoup(urllib.urlopen(url))
与:
return BeautifulSoup(html, 'html.parser')
现在如果我这样做:
print attendances_table
我只得到:
<table border="0" cellpadding="10" cellspacing="0" width="850">
<tr><td valign="TOP" width="42%">
<p><b><u>Belgium</u></b></p></td></tr></table>
我应该改变什么?
【问题讨论】:
-
不同的解析器解析不同。
lxml对我来说很好,html5lib也是如此。 -
html5lib不起作用,但lxml似乎起作用。再次感谢 ;)。解决了 -
是的,
html5lib树构建器创建树的方式似乎存在错误,其中有一个悬空链接(None而不是下一个元素)。跨度>
标签: python html parsing html-parsing beautifulsoup