Kindly find the following link:
http://www.bseindia.com/stock-share-price/stockreach_financials.aspx?scripcode=505200&expandable=0
I have tried the following:
from bs4 import BeautifulSoup as soup
import csv
from pandas import read_csv
import requests
file_path=r'C:\Users\PreciseT3\Desktop\EicherStockDetails.csv'
eicher_stock_url='http://www.bseindia.com/stock-share-price/stockreach_financials.aspx?scripcode=505200&expandable=0'
get_url=requests.get(eicher_stock_url)
target_table=soup(get_url.text,'lxml')
extracted_table_data=target_table.find('table',id='acr')
datasets=[]
col_names=[]
count=1
with open(file_path,'r+') as file:
writer=csv.writer(file)
col_names.append('Years')
for years_row in extracted_table_data.find('tr').find_all('td',class_='TTHeader'):
if not(years_row.get_text()=='(in Cr.)'):
print(years_row.get_text())
col_names.append(years_row.get_text())
writer.writerow(col_names)
with open(file_path,'r+') as file:
writer=csv.writer(file)
for row_headings in extracted_table_data.find('tr').find('td',class_='TTRow_left'):
col_names.append(row_headings)
for row_values in extracted_table_data.find('tr').find_all('td',class_='TTRow_right',text=lambda x:'6,188.03' in x or '3,031.22' in x or '1,702.47' in x or '1,049.26' in x or '670.95' in x):
col_names.append(row_values.get_text())
writer.writerow(col_names)
and my result is as follows:
Years,2016,2014,2013,2012,2011,Revenue,"6,188.03","3,031.22","1,702.47","1,049.26",670.95
My Requirements are:
Instead of ((in Cr.)) column name i need to change that as 'year'
I need to explore it and want to get the data as csv formatted file(write into csv supported file) and also i want to Transpose(T) the rows and columns
I need to add an extra columns from another html page(Require some examples)
Please kindly help me.I cannot go further. Thanks in advance.
I've modified this code a bit - but the logic should be straightforward to follow. I've used Cr and Year as the splitter for this fundamental analysis data but you can convert that to millions/qtr as well by adjusting the "main_split" part of the code.
from bs4 import BeautifulSoup
import urllib2
import pandas as pd
url = 'http://www.bseindia.com/stock-share-price/stockreach_financials.aspx?scripcode=505200&expandable=0'
html = urllib2.urlopen(url).read()
soup = BeautifulSoup(html, "html.parser")
main = []
for tr in soup.findAll('tr'):
mainSub = []
for td in tr.findAll('td'):
mainSub += [td.text]
main += [mainSub]
splitter = []
for y in range(len(main)):
splitter += [any('--' in x for x in main[y])]
split_index = [x for x in range(len(splitter)) if splitter[x] == True]
main_split = main[(split_index[7]+2):(split_index[8]-2)]
main_zip = zip(*main_split)
DF = pd.DataFrame(main_zip,columns=[x.replace(' ', '_') for x in main_zip.pop(0)])
print DF
Hope this helps. Cheers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.