lxml.html parsing with XPath and variables

后端 未结 2 1971
被撕碎了的回忆
被撕碎了的回忆 2021-01-02 17:16

I have this HTML snippet

Table of Contents

2条回答
  •  旧时难觅i
    2021-01-02 17:45

    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]
    

提交回复
热议问题