python - 如何访问路径响应
问题描述
我试图从雅虎财经那里抢到股价。我不太了解 Xpaths,所以我不知道如何访问返回的值
from lxml import html
import requests
r = requests.get('https://uk.finance.yahoo.com/quote/BVXP.L?p=BVXP.L')
root = html.fromstring(r.content)
price = root.xpath('//*[@id="quote-header-info"]/div[3]/div/div/span[1]')
我已经在 chrome 中使用 XpathHelper 检查了 xpath,它返回了我正在寻找的值(3,325.00 - 或者当时的报价。)
但是在 Python 中,我不知道如何访问该信息。
print(price)
# Returns [<Element span at 0x108832278>]
正确的做法是什么?
解决方案
尝试print(price[0].text)
,您会看到 price 是一个包含一个项目的列表,只需通过索引我们就可以访问您想要的信息。
仅供参考,如果您遇到困难,请始终尝试查看它拥有的方法,print(dir(price[0]))
这只是您示例中的一个说明,但仅通过查看对象的方法和属性,它就可以让我们一瞥,前进的方向。
推荐阅读
- api - Photoshop API 广播命令
- azure-devops-server-2019 - 将值复制到 tfs xml 中的只读字段
- c# - 如何在 Telerik 中使用分页获取 RadGridView 项目?
- android - 我正在使用 https://github.com/network-international/payment-sdk-android/ url 进行 N-genious 付款,但项目未运行
- r - R中纵向数据的正态性
- google-cloud-platform - Google Cloud Platform - 您似乎无权访问项目 [shopify-275621] 或它不存在
- hibernate - quarkus 反应式命名数据源 em 为空
- python - ffmpeg 流在 5 分钟后中断
- c# - 在 appveyor 中的 UI 上运行 Xunit 测试
- javascript - 如何使“精选”边框的两端倾斜?