我有以下 html 代码:<div class="xyOfqd"><div class="aAAD"> <div class="Bgbcca">Updated</div> <span class="hthtb"> <div> <span class="hthtb">September 30, 2018</span> </div> </span></div><div class="aAAD"> <div class="Bgbcca">Text1</div> <span class="hthtb"> <div><span class="hthtb">Text2</span></div> </span></div><div class="aAAD"> <div class="Bgbcca">MyText</div> <span class="hthtb"> <div> <span class="hthtb">Text3</span> </div> </span></div><div class="aAAD"> <div class="Bgbcca">Text4</div> <span class="hthtb"> <div><span class="hthtb">Text5</span></div> </span></div><div class="aAAD"> <div class="Bgbcca">Text6</div> <span class="hthtb"> <div><span class="hthtb">Text7</span></div> </span></div><div class="aAAD"><div class="Bgbcca"> Text8/div> <span class="hthtb"> <div> <span class="hthtb"> <div>Text9</div> <div><a href="https://google.com">Text10</a></div> </span> </div> </span></div><div class="aAAD"> <div class="Bgbcca">Text11</div> <span class="hthtb"> <div><span class="hthtb">Text12</span></div> </span></div>如何找到Text3紧跟在div带有 的元素之后的元素MyText?
3 回答
POPMUISE
TA贡献1765条经验 获得超5个赞
您可以使用lxml.html解决方案:
from lxml import html
source = """
<div class="xyOfqd">
<div class="aAAD">
<div class="Bgbcca">Updated</div>
...
<span class="hthtb">
<div><span class="hthtb">Text12</span></div>
</span>
</div>"""
tree = html.fromstring(source)
print(tree.xpath('//div[.="MyText"]/following-sibling::span/div/span/text()'))
添加回答
举报
0/150
提交
取消