你很亲密 使用
text()=而不是
@text(表示属性)。
e = root.xpath('.//a[text()="TEXT A"]')或者,如果您仅知道文本包含“ TEXT A”,
e = root.xpath('.//a[contains(text(),"TEXT A")]')或者,如果您仅知道文本以“ TEXT A”开头,
e = root.xpath('.//a[starts-with(text(),"TEXT A")]')有关可用字符串函数的更多信息,请参见文档。
例如,
import lxml.html as LHtext = '''<html> <body> <a href="/1234.html">TEXT A</a> <a href="/3243.html">TEXT B</a> <a href="/7445.html">TEXT C</a> <body></html>'''root = LH.fromstring(text)e = root.xpath('.//a[text()="TEXT A"]')print(e)产量
[<Element a at 0xb746d2cc>]



