Skip to content

Instantly share code, notes, and snippets.

@sandeepnmenon
Created February 23, 2020 17:39
Show Gist options
  • Save sandeepnmenon/b4eacde6c2b84ed7cafebac81caa2f97 to your computer and use it in GitHub Desktop.
Save sandeepnmenon/b4eacde6c2b84ed7cafebac81caa2f97 to your computer and use it in GitHub Desktop.
Download Bhavcopy files
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import os"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Give start year and end year"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"startYear = 2018\n",
"endYear = 2020"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Choose months (leave as is if you need all months)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"months = ['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for year in range(startYear,endYear+1):\n",
" for month in months:\n",
" for day in range(1,31):\n",
" folder = f\"./Bhavcopy/{year}/\"\n",
" if not os.path.exists(os.path.dirname(folder)):\n",
" os.makedirs(os.path.dirname(folder))\n",
" \n",
" dayStr = f\"0{day}\" if day <10 else day\n",
" url = f\"https://www1.nseindia.com/content/historical/EQUITIES/{year}/{month}/cm{dayStr}{month}{year}bhav.csv.zip\"\n",
" try:\n",
" r = requests.get(url)\n",
" if(r.status_code == 200):\n",
" open(f'{folder}/{month}{dayStr}.zip', 'wb+').write(r.content)\n",
"\n",
" except Exception:\n",
" print(Exception.Message)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment