lxml etree ElementUnicodeResult类的getparent()获取到的是前一个节点,而不是父节点

发布于 2022-09-06 08:14:10 字数 2682 浏览 17 评论 0

html数据

<p class="para">
    如果可选的第三个参数
    <code class="parameter">strict</code>
    为
    <strong>
        <code>TRUE</code>
    </strong>
    ,则
    <span class="function">
        <strong>array_search()</strong>
    </span> 
    将在
    <code class="parameter">haystack</code>
    中检查
    <em class="emphasis">完全相同</em>的元素。这意味着同样严格比较
    <code class="parameter">haystack</code> 里
    <code class="parameter">needle</code> 的
    <a href="language.types.php" class="link">类型</a>
    ,并且对象需是同一个实例。
</p>

方法

def get_code(self, node, active=False):
    c = ''
    if isinstance(node, etree._ElementUnicodeResult):
        c = str(node).replace('\n', '').replace(' ', '').strip()
        p = node.getparent()
        print(p.tag + ' : ' + str(p.attrib) +'  | content: '+ c)
        return c
    if isinstance(node, etree._Element):
        for item in node.xpath('node()'):
            c += str(get_code(item, active))
        return c

使用上面的方法递归读取每个节点的文本内容,xpath('node()')匹配文本为lxml.etree.ElementUnicodeResult对象,节点为lxml.etree.Element对象。

混合排版的文本ElementUnicodeResult对象getparent()不能正确获取父节点,前面有兄弟节点时getparent()获取到的时前边的兄弟节点。比如文本 ,则 将在 中检查 都是获取到文字前边的的节点

前边方法的输出

p : {'class': 'para'}  content: 如果可选的第三个参数
code : {'class': 'parameter'}  content: strict
code : {'class': 'parameter'}  content: 为
code : {}  content: TRUE
strong : {}  content: ,则
strong : {}  content: array_search()
span : {'class': 'function'}  content: 将在
code : {'class': 'parameter'}  content: haystack
code : {'class': 'parameter'}  content: 中检查
em : {'class': 'emphasis'}  content: 完全相同
em : {'class': 'emphasis'}  content: 的元素。这意味着同样严格比较
code : {'class': 'parameter'}  content: haystack
code : {'class': 'parameter'}  content: 里
code : {'class': 'parameter'}  content: needle
code : {'class': 'parameter'}  content: 的
a : {'href': 'http://php.net/manual/zh/language.types.php', 'class': 'link'}  content: 类型
a : {'href': 'http://php.net/manual/zh/language.types.php', 'class': 'link'}  content: ,并且对象需是同一个实例。

这个如何解决。

我的目的是根据他的父节点的标签 判断文字是粗体 还是斜体
期待结果应该为

如果可选的第三个参数`strict` 为 **`TRUE`**,则**array_searck**中检查*完全相同*的元素。这意味着同样严格比较`haystack`里`needle`的类型,并且对象需是同一个实例。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别把无礼当个性 2022-09-13 08:14:10

审核时间好长!!!!
搜索一圈没找到解决。临时解决方法传入父节点,递归每层深度的父节点是一样的

def get_code(self, node, parent=None, active=False):
    if parent is None:
        parent = node.getparent()
    c = ''
    if isinstance(node, etree._ElementUnicodeResult):
        c = str(node).replace('\n', '').replace(' ', '').strip()
        p = parent
        print(p.tag + ' : ' + str(p.attrib) +'  | content: '+ c)
        return c
    if isinstance(node, etree._Element):
        for item in node.xpath('node()'):
            c += str(get_code(item,node,active=active))
        return c
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文