Python 使用Beautiful Soup解析NELL Knowlege主页
我正在使用Beauty Soup解析来自的类别列表,我得到了此页面的html代码:Python 使用Beautiful Soup解析NELL Knowlege主页,python,html,parsing,beautifulsoup,html-parsing,Python,Html,Parsing,Beautifulsoup,Html Parsing,我正在使用Beauty Soup解析来自的类别列表,我得到了此页面的html代码: <html> <head> <link href="../css/browser.css" rel="stylesheet" type="text/css"/> <script type="text/javascript"> if (parent.location.href == self.locati
<html>
<head>
<link href="../css/browser.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
if (parent.location.href == self.location.href) {
if (window.location.href.replace)
window.location.replace('index.php');
else
// causes problems with back button, but works
window.location.href = 'index.php';
}
</script>
</head>
<body id="ontology">
...
</body>
</html>
为什么在这种情况下head元素没有兄弟元素?我得到: AttributeError:“非类型”对象没有属性“下一个元素” 当尝试获取
标题时。下一个同级也是。这是因为文本节点也是内容的一部分
使用来定位类别列表,而不是操作contents
属性。例如,以下是如何列出顶级类别:
for li in soup.select("body#ontology > ul > li"):
print li.find_all("a")[-1].text
@MaximRukhlov是的,在BeautifulSoup
中定位元素有多种方法。我向您指出CSS选择器的原因是,我非常确定这将帮助您解析类别列表。
for li in soup.select("body#ontology > ul > li"):
print li.find_all("a")[-1].text