[英]Parsing html page with lxml in python
我想用python中的lxml解析此Xpath查詢。
.//*[@id='content_top']/article/div/table/tbody/tr[5]/td/p/text()
我檢查了Firepath(xpath的firebug擴展)中的xpath查詢,它可以工作,但是我的python代碼什么都沒顯示。 這是來源。
from lxml import html
import requests
page = requests.get("http://www.scienzeetecnologie.uniparthenope.it/avvisi.html")
tree = html.fromstring(page.text)
avvisi = tree.xpath(".//*[@id='content_top']/article/div/table/tbody/tr[5]/td/p/text()")
print(avvisi)
輸出為"[]"
。
源html中沒有實際的<tbody>
元素,它只是HTML解析器添加的DOM中的元素。
firebug實際上顯示了DOM(我猜是firepath,這是在此DOM上運行的firebug擴展(而不是源html))。
有關<tbody>
詳細說明以及firebug為什么顯示它,請查看SO問題的答案- 為什么firebug將<tbody>添加到<table>? 還是這個問題- 為什么瀏覽器將tbody元素插入表元素?
在您的情況下,從xpath中刪除<tbody>
可以使其正常工作,例如-
avvisi = tree.xpath(".//*[@id='content_top']/article/div/table/tr[5]/td/p/text()")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.