How to find direct children of element in lxml

后端 未结 3 1282
走了就别回头了
走了就别回头了 2021-01-14 09:00

I found an object with specific class:

THREAD = TREE.find_class(\'thread\')[0]

Now I want to get all

elements that a

3条回答
  •  隐瞒了意图╮
    2021-01-14 10:01

    I'm not sure, but it seem that your problem is in HTML itself: note that there are couple Tag omission cases applicable for p nodes, so closing tags of paragraphs

    first

    second

    simply ignored by parser and both nodes identified as siblings, but not parent and child, e.g.

    first

    second

    So XPath //div[@class="thread"]/p will return you both paragraphs

    You can simply replace p tags with div tags and you'll see different behaviour:

    first
    second

    Here //div[@class="thread"]/div will return first node only

    Please correct me if my assumption is incorrect

提交回复
热议问题