请求无法从网页获取文本?

2023-12-14

我正在尝试从网页获取 VIX 的值。

我正在使用的代码:

raw_page = requests.get("https://www.nseindia.com/live_market/dynaContent/live_watch/vix_home_page.htm").text
soup = BeautifulSoup(raw_page, "lxml")
vix = soup.find("span",{"id":"vixIdxData"})
print(vix.text)

这给了我:

' '

如果我看到维克斯,

<span id="vixIdxData" style=" font-size: 1.8em;font-weight: bold;line-height: 20px;">/span>

在网站上,元素有文本,

<span id="vixIdxData" style=" font-size: 1.8em;font-weight: bold;line-height: 20px;">15.785/span>

The 15.785value 是我想通过使用 requests 获得的值。


您正在查找的数据在页面源中不可用。和requests.get(...)仅获取页面源代码,不包含通过 JavaScript 动态添加的元素。但是,您仍然可以使用它来获取它requests module.

在开发者工具的“网络”选项卡中,您可以看到一个名为VixDetails.json。请求正在发送至https://www.nseindia.com/live_market/dynaContent/live_watch/VixDetails.json,它以 JSON 形式返回数据。

enter image description here

您可以使用内置的.json()的功能requests module.

r = requests.get('https://www.nseindia.com/live_market/dynaContent/live_watch/VixDetails.json')
data = r.json()
vix_price = data['currentVixSnapShot'][0]['CURRENT_PRICE']
print(vix_price)
# 15.7000
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

请求无法从网页获取文本? 的相关文章

随机推荐