-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindividual_company_stock.py
More file actions
42 lines (33 loc) · 1.9 KB
/
individual_company_stock.py
File metadata and controls
42 lines (33 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from io import StringIO
import requests
import pandas as pd
from datetime import datetime , timedelta
import bs4
session = requests.session()
headers = {
"user-agent": "Chrome/87.0.4280.88"
}
head = {
'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/87.0.4280.88 Safari/537.36 "
}
def getHistoryData(company, from_date=(datetime.today().strftime("%d-%m-%Y")), to_date=(datetime(datetime.today().year - 1, datetime.today().month,datetime.today().day).strftime("%d-%m-%Y"))):
session.get("https://www.nseindia.com", headers=head)
session.get("https://www.nseindia.com/get-quotes/equity?symbol=" + company, headers=head) # to save cookies
session.get("https://www.nseindia.com/api/historical/cm/equity?symbol="+company, headers=head)
url = "https://www.nseindia.com/api/historical/cm/equity?symbol=" + company + "&series=[%22EQ%22]&from=" + from_date + "&to=" + to_date + "&csv=true"
webdata = session.get(url=url, headers=head)
df = pd.read_csv(StringIO(webdata.text[3:]))
return df
def niftyHistoryData(varient, from_date = ((datetime(datetime.today().year - 1, datetime.today().month, datetime.today().day) + timedelta(days=2)).strftime("%d-%m-%Y")), to_date =(datetime.today().strftime("%d-%m-%Y"))):
varient = varient.upper()
varient = varient.replace(' ', '%20')
varient = varient.replace('-', '%20')
webData = session.get(
url="https://www1.nseindia.com/products/dynaContent/equities/indices/historicalindices.jsp?indexType=" + varient +
"&fromDate=" + from_date + "&toDate=" + to_date,
headers=head)
soup = bs4.BeautifulSoup(webData.text, 'html5lib')
return pd.read_csv(StringIO(soup.find('div', {'id': 'csvContentDiv'}).contents[0].replace(':','\n')))
# print(getHistoryData('SHREECEM',from_date='30-04-2020',to_date='30-04-2021'))
# print(niftyHistoryData('NIFTY 50'))