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'])