python - 我想从给定的网页中提取 vwap 值

标签 python html web-scraping

from bs4 import BeautifulSoup as BS
url="https://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=KOTAKBANK&illiquid=0&smeFlag=0&itpFlag=0"

page=urllib.request.Request(url,headers={'User-Agent': 'Mozilla/5.0'}) 
infile=urllib.request.urlopen(page).read()
data = infile.decode('ISO-8859-1') # Read the content as string decoded with ISO-8859-1


print (soup.find('span', {'id':'vwap'}).text)

我正在 jupyter 中运行此代码;我希望输出在网页上显示为 VWAP 的值,但没有显示任何内容。

最佳答案

您可以从另一个标签中提取,该标签的字符串包含许多有用的信息,包括使用 json 加载后的价格

import requests
import json
from bs4 import BeautifulSoup as bs

r = requests.get('https://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=KOTAKBANK&illiquid=0&smeFlag=0&itpFlag=0')
soup = bs(r.content, 'lxml')
data = json.loads(soup.select_one('#responseDiv').text.strip())
print(data['data'][0]['averagePrice'])

关于python - 我想从给定的网页中提取 vwap 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55942381/

相关文章:

python - 在python中组合2个列表列表

python - 在Python列表[Dict]中按字典ID查找元素

c# - 如何将 HTML 读取为 XML?

javascript - KnockoutJS 未绑定(bind)

php - 回显复选框、单选按钮和文本框输入的值

php - 在 Controller 中使用 Goutte 和 Symfony2

python - 带文本的 Tkinter 基本布局

python - 在 Django 用户之间共享对象

html - 使用 BeautifulSoup 或 XPATH 获取内容属性值对

javascript - 使用 CasperJS 和 PhantomJS 抓取 Google 关键字工具