Как преобразовать переменную lxml Element в xpath, используя python - PullRequest
0 голосов
/ 27 сентября 2018
from lxml.html import fromstring
#### Example HTML ####
text = '''
    <h4>Text</h4>
    <p>These is first Paragraph</p>
    <p>These is second Paragraph</p>
    <h4>title</h4>
    <p>casting of lxml element in xpath expression</p>
    <h4>Conclusion</h4>
    <p>Can any one give me a solution</p>
'''
tree = fromstring(text)
for i in tree.xpath('//h4'):
    ### getting the next h4 element####
    next_h4 = i.xpath('./following-sibling::h4[1]')
    #### need to get the text##
    text = i.xpath('./following-sibling::p[following-sibling::next_h4]//text()]')
    ## In the above xpath need to cast the lxml Element ##

Я пытался подписаться на xpath

text = i.xpath ('./ follow-sibling :: p [follow-sibling :: "% s"]]% next_h4')

и

text = i.xpath ('./ follow-sibling :: p [follow-sibling :: $ nex]]', $ nex = next_h4)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...