请问如何提取br标签内的数据

通过etree对象提取br标签内的数据返回值为空

<div class="msg">
    <br>
    日期&nbsp;数据1&nbsp;数据2
    <br>
    2022-06-28&nbsp;3.4&nbsp;3.5
    <br>
</div>
from lxml import etree

host_list = tree.xpath('//*[@class="msg"]/br')
for host in host_list:
    res_text = host.xpath('./br/text()')
Jason990420
最佳答案

通过 etree 对象提取 br 标签内的数据返回值为空

发现 br 标签内的数据我可以直接使用 host.tail 直接获取到

标签内没有数据 .... The <br> tag is an empty tag which means that it has no end tag.

tail

Text after this element's end tag, but before the next sibling element's start tag. This is either a string or the value None, if there was no text.

host.tail 获取 br 後面的数据, 而不是它本身的数据.

2年前 评论
miusa166 (楼主) 2年前
讨论数量: 3

debug后发现br标签内的数据我可以直接使用host.tail直接获取到,对此不是很理解,请各位指点一下

from lxml import etree

host_list = tree.xpath('//*[@class="msg"]/br')
for host in host_list:
    res_text = host.tail
2年前 评论
Jason990420

通过 etree 对象提取 br 标签内的数据返回值为空

发现 br 标签内的数据我可以直接使用 host.tail 直接获取到

标签内没有数据 .... The <br> tag is an empty tag which means that it has no end tag.

tail

Text after this element's end tag, but before the next sibling element's start tag. This is either a string or the value None, if there was no text.

host.tail 获取 br 後面的数据, 而不是它本身的数据.

2年前 评论
miusa166 (楼主) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!