尝试使用python提取股票的最新交易价格?

问题描述

我正在尝试使用下面的代码提取股票的LTP。它没有返回任何值。请在下面的代码中告诉错误:

from selenium import webdriver
from bs4 import BeautifulSoup


driver=webdriver.Chrome("C:\\Users\\hp\\Desktop\\New folder\\chromedriver.exe")

input_keyword=input("enter the stock name/symbol/keyword:")

url="https://www.nseindia.com/search?q="+input_keyword

driver.get(url)

content=driver.page_source


soup=BeautifulSoup(content,"html.parser")

details=soup.find_all("div",class_="searchWrp")

print("Choose from the followings:")

for data in details:
    name=data.div.a.text.replace("\n","").replace(" ","")
    print(name)

driver.close()

stock_name=input("enter the stock name from above:")

driver=webdriver.Chrome("C:\\Users\\hp\\Desktop\\New folder\\chromedriver.exe")

url_stock="https://www.nseindia.com/get-quotes/equity?symbol="+stock_name

driver.get(url_stock)

content_stock=driver.page_source

soup_stock=BeautifulSoup(content_stock,"html.parser")

details_stock=soup.find_all("div",attrs={"class":"blkbox-whitetxt"})
for price in details_stock:
    LTP=price.find(span).text
    print(LTP)

输出


runfile('C:/Users/hp/Desktop/sharemarket.py',wdir ='C:/ Users / hp / Desktop')

输入股票名称/符号/关键字:Axis Bank 从以下选项中选择: 轴银行 欧邦克 银行 HBANKETF UTIBANKETF 企业银行 DCB银行 HDFC银行 ICICIBANKP 银行 清算银行 由UTIAssetManagementCompanyLimited-UTIBankExchangeTradedFund(UTIBank)发行的单位清单 UjjivanSmallFinanceBank上市仪式 由UTIAssetManagementCompanyLimited-UTIBankExchangeTradedFund(UTIBank)发行的单位清单 NiftyBankF&O 银行部门模块 清算银行利率 清算银行-商品衍生品 国际投资银行业务 印度商业银行:ABeginner的模块

从上方输入股票名称:AUBANK


解决方法

最新价格(以及其他信息)是从外部URL加载的。您可以使用以下示例获取它:

import json
import requests


url = 'https://www.nseindia.com/api/quote-equity?symbol=AXISBANK'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0'}

data = requests.get(url,headers=headers).json()

# uncomment this to print all data:
# print(json.dumps(data,indent=4))

print('Last price:',data['priceInfo']['lastPrice'])

打印:

Last price: 466.8

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...