python - python xpath 返回空列表而不是值或文本
问题描述
以下是我的程序,它返回一个空列表,它应该返回值“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)
解决方案
您不应该tbody
在 XPath 中使用标签,因为它实际上并不存在于页面源代码中,而是在页面呈现时由浏览器添加。跳过它:
.../table[2]/tbody/tr[1]...
->.../table[2]//tr[1]...
推荐阅读
- python - WARNING:tensorflow:Layer my_model 将输入张量从 dtype float64 转换为 layer 的 float32 dtype,这是 TensorFlow 2 中的新行为
- jquery - 浏览器 XMLHttpRequest 中的 Jquery 问题?
- sqlite - SQLite 从表中删除未选择的行
- node.js - Mongoose 无法连接到 Mlab
- opengl - OpenGL 无绑定纹理和无缝立方体贴图过滤
- solace - 是否可以在 Solace 中检测到端点的订阅者的存在
- iframe - 使用 javascript 更改输入类型,无处不在
- python - 将值从 panda 数据帧传递到 http 请求
- python - 如何安装“pymprog”?蟒蛇 3.7
- webpack - 如何使用 Webpack 的 http-proxy-middleware 将 _some_ 请求代理到“/”?