About the Projects
Fetch the current share price of multiple companies using Python web scraping
This project describes a Python code, which fetches the current stock price for multiple listed companies. This script first reads the excel sheet to get the list of all companies for which CMP (Current Market Price) has to be calculated and then fire up the browser to open a website which has this information. The automation script enters the name of each stock, one by one, in the search bar which in turn load the page with particular information, from which the CMP is scraped and stored.
This continues until the lists items from the excel sheet get over. Parallelly, scripts also keep on calculating the total profit/loss for each share by comparing it against the Invested price and number of shares. In the end, profit and (or) loss is calculated for each share
and the information is written back to the excel sheet. Excel sheet does further calculation to build up the bar graph for representing the data in the best possible way. Look at the video to get complete information.
YouTube video
Python code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import openpyxl
from pathlib import Path
wait_imp = 10
excel_path = Path(r"D:\Learning\Practice\Selenium\Stock\stocks_data.xlsx")
wb = openpyxl.load_workbook(excel_path)
ws = wb["CMP"]
# Read company name from excelsheet
print ("Step 1 --> Reading Excel-sheet, Please wait....")
s_row = 4
c_list = []
avg_val = []
qnty_list = []
while ws.cell(row = s_row, column= 2).value != None:
c_name = ws.cell(row = s_row, column= 2).value
val_1 = ws.cell(row = s_row, column= 4).value
qnty = ws.cell(row = s_row, column= 5).value
c_list.append(c_name)
avg_val.append(val_1)
qnty_list.append(qnty)
s_row += 1
print ("Company name available in Database")
[print(' ->',name) for name in c_list]
time.sleep(2)
print ('\n')
# create a webdriver object for chrome-option and configure
CO = webdriver.ChromeOptions()
CO.add_experimental_option('useAutomationExtension', False)
CO.add_argument('--ignore-certificate-errors')
CO.add_argument('--start-maximized')
wd = webdriver.Chrome(r'D:\Learning\Practice\Selenium\chromedriver.exe',options=CO)
print ("Step 2 --> Opening Finance website\n")
wd.implicitly_wait(wait_imp)
wd.get("https://www.moneycontrol.com")
time.sleep(5)
print ("******************************************************************************")
print (" Getting Live Stock Value !! Please wait ...\n")
for i in range(len(c_list)):
src = wd.find_element_by_id ("search_str")
src.send_keys(c_list[i])
src.send_keys(Keys.RETURN)
wd.implicitly_wait(wait_imp)
s_v = wd.find_element_by_xpath("//*[@id='div_nse_livebox_wrap']/div[1]/div[1]/div/div[2]/span[1]")
ws.cell(row=4+i, column= 3, value = s_v.text)
diff = (avg_val[i] - float(s_v.text))* qnty_list[i]
per_diff = (diff/(avg_val[i]*qnty_list[i]))*100
print ("{:>23} -> CMP {:<7} Current P/L->[{:>8.2f}] %P/L -> {:>6.2f}%".format(c_list[i],s_v.text, diff, per_diff))
print ('\n')
print ("Step 3 --> Writing Latest Price into Excel-sheet ....\n")
time.sleep(1)
wb.save(excel_path)
print ("Step 4 --> Successfully Written \n")
print ("Step 5 --> Closing browser !\n")
print (" ----------------------- FINISHED !! ------------------------")
time.sleep(1)
wd.close()
Can you explain this line in a little more detail?
print (“{:>23} -> CMP {:[{:>8.2f}] %P/L -> {:>6.2f}%”.format(c_list[i],s_v.text, diff, per_diff))
Hello Anonymous,
This is string formatting that has been done to align the results in a specific manner. For example
:>8.2f means align towards the right side, to 8 spaces and up to 2decimal points of floating value.
hi can you please upload the excel file as well ?
Hello Anh,
Thank you for the suggestion, Excel file has been uploaded on the project page. You can download for your reference.
File “c:\Users\user\Desktop\my code\scrap.py”, line 53, in
diff = (avg_val[i] – float(s_v.text))* qnty_list[i]
ValueError: could not convert string to float:
this is the error am getting tried many times plz check and do needful
I am getting this error…
—————————————————————————
ModuleNotFoundError Traceback (most recent call last)
in
—-> 1 from selenium import webdriver
2 from selenium.webdriver.common.keys import Keys
3 import time
4 import openpyxl
5 from pathlib import Path
ModuleNotFoundError: No module named ‘selenium’
Hello Tushar,
It seems like you have not installed selenium library (or package) in your system. Please do so and have the correct version of chrome webdriver. You will be good to go.
Hello Tushar,
I worked on your example which is pretty interesting, but unfortunately I got some errors and I’m not able to fix them.
Maybe it’s caused by different versions of python or lib’s?
Hope you can gibe me some advice?
******************************************************************************
Getting Live Stock Value !! Please wait …
******************************************************************************
c_list= [‘Yes Bank’, ‘State Bank ofIndia’, ‘IRCTC’, ‘Canara Bank’, ‘Jubilant Life Sciences’, ‘Apollo Tyres’]
c_list[i] Yes Bank
Traceback (most recent call last):
File “\\lene590bl\Daten\20_Daten\600_Progr\Python\0200_Fortges_prgs\Stock_market\stock_v2.py”, line 77, in
s_v = wd.find_element_by_xpath(“//*[@id=’div_nse_livebox_wrap’]/div[1]/div[1]/div/div[2]/span[1]”)
File “C:\Program Files (x86)\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File “C:\Program Files (x86)\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File “C:\Program Files (x86)\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 321, in execute
self.error_handler.check_response(response)
File “C:\Program Files (x86)\lib\site-packages\selenium\webdriver\remote\errorhandler.py”, line 242, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: no such element: Unable to locate element: {“method”:”xpath”,”selector”:”//*[@id=’div_nse_livebox_wrap’]/div[1]/div[1]/div/div[2]/span[1]”}
(Session info: chrome=91.0.4472.77)
Regards Burkhard
I am also facing the same issue. Its probably because the script is not able to locate “div_nse_livebox_wrap” in the moneycontrol website.
Hello Guys,
I completely understand the scenario and would like to update on the same. Actually moneycontrol website has changed its structure and it is also not allowing the crawling from any kind of automation script. I would advise you to try yahoo finance rather.
Hope you will get success, I have already tried that one and if you want I can share the script as well as create a video on the same.
Can you please upload the video using yahoo finance. Not able to crawl on yahoo finances using this code.
Not able to locate the attached excel file kindly guide in this regard
Can you please upload the video using yahoo finance. Not able to crawl on yahoo finances using this code.
can share the xl file link
Sir please give location of excel file download
Sir please share the full code of the software using yahoo finance
Traceback (most recent call last):
File “c:\Users\chand\OneDrive\Desktop\proj contest\sri.py”, line 10, in
wb = openpyxl.load_workbook(excel_path)
File “C:\Users\chand\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\reader\excel.py”, line 315, in load_workbook
reader = ExcelReader(filename, read_only, keep_vba,
File “C:\Users\chand\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\reader\excel.py”, line 124, in __init__
self.archive = _validate_archive(fn)
File “C:\Users\chand\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\reader\excel.py”, line 96, in _validate_archive
archive = ZipFile(filename, ‘r’)
File “C:\Users\chand\AppData\Local\Programs\Python\Python310\lib\zipfile.py”, line 1249, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: ‘C:\\Users\\chand\\OneDrive\\Documents\\sri.xlsx’