Skip to content

Instantly share code, notes, and snippets.

@meadhikari
Created May 10, 2013 00:47
Show Gist options
  • Save meadhikari/5551703 to your computer and use it in GitHub Desktop.
Save meadhikari/5551703 to your computer and use it in GitHub Desktop.
import requests
from dateiterator import daterange
from BeautifulSoup import BeautifulSoup
def dataOf(date):
stockdata = []
postdata = {'Date':date}
r = requests.post("http://www.nepalstock.com/datanepse/previous.php",data=postdata)
content = r.text
soup = BeautifulSoup(content)
bigtable = str(soup.findAll('table')[4])
datatable = BeautifulSoup(bigtable).findAll('table')[5]
for row in datatable.findAll('tr')[1:]:
col = row.findAll('td')
S_No = col[0].string
Traded_Company = col[1].a.string
No_of_Transaction = col[2].string
Max_Price = col[3].string
Min_Price = col[4].string
Closing_Price = col[5].string
Total_Share = col[6].string
Amount = col[7].string
Previous_Closing = col[8].string
Difference_Rs = col[9].string
record = (str(date),S_No,Traded_Company,No_of_Transaction,Max_Price,Min_Price,Closing_Price,Total_Share,Amount,Previous_Closing,Difference_Rs)
stockdata.append(record)
return stockdata
text_file = open("Data.txt", "w")
for i in daterange():
try:
data = dataOf(i)
except:
continue
for i in data:
text_file.write(i[0]+","+ i[1]+","+i[2]+","+i[3]+","+i[4]+","+i[5]+","+i[6]+","+i[7]+","+i[8]+","+i[9]+","+i[10])
print i[0]
text_file.write("\n")
text_file.close()
#dateiterator.py
from datetime import date,timedelta
begin = date(1999,07,18)
end = date(2013,02,19)
def daterange(begin=begin, end=end, delta = timedelta(1)):
"""Form a range of dates and iterate over them.
Arguments:
begin -- a date (or datetime) object; the beginning of the range.
end -- a date (or datetime) object; the end of the range.
delta -- (optional) a timedelta object; how much to step each iteration.
Default step is 1 day.
Usage:
"""
if not isinstance(delta, timedelta):
delta = timedelta(delta)
ZERO = timedelta(0)
if begin < end:
if delta <= ZERO:
raise StopIteration
test = end.__gt__
else:
if delta >= ZERO:
raise StopIteration
test = end.__lt__
while test(begin):
yield begin
begin += delta
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment