Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Beautiful Soup解析NELL Knowlege主页_Python_Html_Parsing_Beautifulsoup_Html Parsing - Fatal编程技术网

Python 使用Beautiful Soup解析NELL Knowlege主页

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

我正在使用Beauty Soup解析来自的类别列表,我得到了此页面的html代码:

<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