<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div class="class1">
<div class="class2">
a
<div class="class3">div1</div>
</div>
<div class="class2">
b
<div class="class3">div2</div>
</div>
<div class="class2">
c
<div class="class3">div3</div>
</div>
</div>
</body>
</html>
使用text()定位输出第二个class2的内容
# 使用contains模糊匹配
html.xpath("//div[@class='class3'][contains(text(),'div2')]/../text()")
# 输出
['\n b\n ', '\n ']
# 使用精确匹配
html.xpath("//div[@class='class3'][text()='div2']/../text()")
# 输出
['\n b\n ', '\n ']