您可以使用
getpath()从获取xpath
element,例如:
import requestsfrom lxml import htmlpage = requests.get("http://www.w3schools.com/xpath/")root = html.fromstring(page.text)tree = root.getroottree()result = root.xpath('//*[. = "XML"]')for r in result: print(tree.getpath(r))
输出:
/html/body/div[3]/div/ul/li[10]/html/body/div[3]/div/ul/li[10]/a/html/body/div[4]/div/div[2]/div[2]/div[1]/div/ul/li[2]/html/body/div[5]/div/div[6]/h3/html/body/div[6]/div/div[4]/h3/html/body/div[7]/div/div[4]/h3
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)