Last active
April 3, 2017 02:40
-
-
Save phaneesh/bf2210dea6311561882a2e81918f8822 to your computer and use it in GitHub Desktop.
Sync IFSC from RBI Dataset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import requests | |
from bs4 import BeautifulSoup | |
import xlrd | |
import shutil | |
import os | |
import codecs | |
page = requests.get("https://www.rbi.org.in/Scripts/bs_viewcontent.aspx?Id=2009") | |
soup = BeautifulSoup(page.content, 'html.parser') | |
ifsc_table = soup.find_all('table', class_='tablebg')[0] | |
ifsc_links = ifsc_table.find_all('a') | |
def download_file(url): | |
local_filename = url.split('/')[-1] | |
print("Downloading file: " + url + " | File Name: " + local_filename) | |
r = requests.get(url.replace("http:", "https:"), stream=True) | |
with open(local_filename, 'wb') as f: | |
shutil.copyfileobj(r.raw, f) | |
return local_filename | |
full_file = codecs.open("all_ifsc.csv", "wb", "utf-8") | |
error_file = codecs.open("all_ifsc_errors.txt", "wb", "utf-8") | |
for link in ifsc_links: | |
file_path = download_file(link['href']) | |
xl = xlrd.open_workbook(file_path) | |
df = xl.sheet_by_index(0) | |
for row in range(1, df.nrows): | |
values = [] | |
try: | |
for c in range(df.ncols): | |
values.append(unicode(df.cell_value(row, c))) | |
print(",".join(values)) | |
print(",".join(values), file=full_file) | |
except: | |
print(df.row(row)) | |
print(df.row(row), file=error_file) | |
os.remove(file_path) | |
full_file.close() | |
error_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment