Created
December 7, 2019 06:40
-
-
Save sakethramanujam/7a726e7f2e9ce0c031e56ce55320fd9b to your computer and use it in GitHub Desktop.
Scraper for from https://www.eraktkosh.in/BLDAHIMS/bloodbank/stockAvailability.cnt
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
# !usr/bin/env python3 | |
from bs4 import BeautifulSoup | |
import sys | |
import pandas as pd | |
import argparse | |
def args(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-f','--filename',help='path to input', type=str) | |
parser.add_argument('-o','--output',help='Path to store the file', type=str) | |
args = parser.parse_args() | |
return args | |
def _row_data(row): | |
tds = row.findAll('td') | |
data = [td.text if td.text else 'Live' for td in tds] | |
return data | |
def _get_data(soup): | |
rows = soup.findAll('tr') | |
data = [_row_data(row) for row in rows] | |
return data | |
def table_data(filename): | |
file = open(filename) | |
soup = BeautifulSoup(file, 'html.parser') | |
data = _get_data(soup) | |
return data | |
def generate_csv(list_data, output_file): | |
cols = ['Sno','Blood Bank','Category','Availability','Last Updated','Type'] | |
df = pd.DataFrame(data=list_data, columns=cols).dropna() | |
df.to_csv(output_file,index=False) | |
print(f'{output_file} Saved!') | |
def main(): | |
filename = args().filename | |
output = args().output | |
table = table_data(filename=filename) | |
generate_csv(list_data=table, output_file=output) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment