lxml.html parsing with XPath and variables

一个人想着一个人 提交于 2019-11-30 14:41:42

Your first example woks, but probably not how you think it shoud:

test=html.xpath("//ul[@class='toc']/li[@class='level2']/div[@class='li']/a/text()='One'")

What this returns is a boolean, which will be true if the condition ...='One' is true for any of the nodes in the result set at the left side of the xpath expression. And that's why you get the error in your second example: True[0] is not valid.

You probalby want all nodes matching the expession, having 'One' as text. The corresponding expression would be:

test=html.xpath("//ul[@class='toc']/li[@class='level2']/div[@class='li']/a[text()='One']")

This returns a nodeset as result, or if you just need the url as a string:

test=html.xpath("//ul[@class='toc']/li[@class='level2']/div[@class='li']/a[text()='One']/@href")
# returns: ['#link1']

I tried mata's response, but for me didn't work:

div_name = 'foo'
my_div = x.xpath(".//div[@id=%s]" %div_name)[0]

I found this on their website http://lxml.de/xpathxslt.html#the-xpath-method for those that might have the same problem :

div_name = 'foo'
my_div = x.xpath(".//div[@id=$name]", name=div_name)[0]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!