python xpath returns 空列表而不是值或文本
python xpath returns empty list instead of the value or text
以下是我的程序,它return是一个空列表,它应该return值'Mar 17',请让我知道我在这里做错了什么。
import requests
from lxml import html
newline="http://www.moneycontrol.com/financials/20microns/balance-sheetVI/2M"
try:
page = requests.get(newline, timeout=5)
except requests.Timeout:
pass
except requests.ConnectionError:
pass
except requests.ReadTimeout:
pass
tree = html.fromstring(page.content)
yrs = tree.xpath('//*[@id="mc_mainWrapper"]/div[3]/div[2]/div[3]/div[2]/div[2]/div[2]/div[1]/table[2]/tbody/tr[1]/td[2]')
print(yrs)
您不应在 XPath 中使用 tbody
标记,因为它实际上不存在于页面源代码中,而是由浏览器在页面呈现时添加的。跳过它:
.../table[2]/tbody/tr[1]...
-> .../table[2]//tr[1]...
以下是我的程序,它return是一个空列表,它应该return值'Mar 17',请让我知道我在这里做错了什么。
import requests
from lxml import html
newline="http://www.moneycontrol.com/financials/20microns/balance-sheetVI/2M"
try:
page = requests.get(newline, timeout=5)
except requests.Timeout:
pass
except requests.ConnectionError:
pass
except requests.ReadTimeout:
pass
tree = html.fromstring(page.content)
yrs = tree.xpath('//*[@id="mc_mainWrapper"]/div[3]/div[2]/div[3]/div[2]/div[2]/div[2]/div[1]/table[2]/tbody/tr[1]/td[2]')
print(yrs)
您不应在 XPath 中使用 tbody
标记,因为它实际上不存在于页面源代码中,而是由浏览器在页面呈现时添加的。跳过它:
.../table[2]/tbody/tr[1]...
-> .../table[2]//tr[1]...