How to find text's Parent Node?

白昼怎懂夜的黑 提交于 2019-12-24 14:33:19

问题


If I use:

import requests
from lxml import html

response = request.get(url='someurl')
tree = html.document_fromstring(response.text)


all_text = tree.xpath('//text()')     # which give all text from page

Inside this all_text list we have all the text from page. Now I want to know if:

text_searched = all_text[all_text.index('any string which is in all_text list')]

Is it possible to get to the web element of the text been searched?


回答1:


You can use getparent() method for this purpose, for example :

.....
.....
all_text = tree.xpath('//text()')

first_text = all_text[0]
parent_element = first_text.getparent()

print html.tostring(parent_element)

Note that the behavior of getparent() might not be the one you expected in case current text element located after element node in the same parent element. Due to the tree model implemented by lxml, the text is considered tail of the preceding element instead of child of the containing element in this case, so getparent() will return the preceding element. See example below to get a clear idea of what I've been talking about :

from lxml import html
raw = '''<div>
    <span>foo</span>
    bar
</div>'''
root = html.fromstring(raw)
texts = root.xpath('//text()[normalize-space()]')
print [t for t in texts]
# output : ['foo', '\n\tbar\n']

[html.tostring(e.getparent()) for e in texts]
# output : ['<span>foo</span>\n\tbar\n', '<span>foo</span>\n\tbar\n']
# see that calling getparent() on 'bar' returns the <span> not the <div>


来源:https://stackoverflow.com/questions/35520709/how-to-find-texts-parent-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!