簡體   English   中英

試圖使用python提取股票的最新交易價格?

[英]Trying to extract Latest Trading price of a stock using 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')

輸入股票名稱/符號/關鍵字:軸銀行選擇以下內容:AXISBANK AUBANK DHANBANK HBANKETF UTIBANKETF CORPBANK DCBBANK HDFCBANK ICICIBANKP RELBANK ClearingBanks ListingofunitsissuedbyUTIAssetManagementCompanyLimited-UTIBankExchangeTradedFund(UTIBank ListingCeremonyofUjjivanSmallFinanceBank ListingofunitsissuedbyUTIAssetManagementCompanyLimited-UTIBankExchangeTradedFund(UTIBank NiftyBankF&O BankingSectorModule ClearingBanks-InterestRateDerivatives ClearingBanks-CommodityDerivatives InvestmentBankingOperations,國際CommercialBankinginIndia :ABeginner'sModule

從上面輸入股票名稱: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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM