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

时间:2019-05-01 20:33:23

标签: 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的值,但输出什么都不会显示。

1 个答案:

答案 0 :(得分:0)

您可以从另一个标签中提取一个标签,该标签包含一个包含很多有用信息的字符串,包括在装入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'])