-
Notifications
You must be signed in to change notification settings - Fork 0
/
Web2Excel.py
40 lines (32 loc) · 1.26 KB
/
Web2Excel.py
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
import pandas as pd
import sys
def Convert2XL(URLofSiteWithTable, xlPath='ConvertedFile.xlsx'):
'''
Downloads the first table from the URL provided
and saves it an XLSX file.
'''
try:
df = pd.read_html(URLofSiteWithTable)[0]
df = df.fillna('')
row_count, column_count = (df.shape)
print('Rows found = %s, Columns found = %s' %
(row_count, column_count))
print(df.head())
SavetoExcel(df, xlPath)
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
lineNo = str(exc_tb.tb_lineno)
print('Error : %s : %s at Line %s.' % (type(e), e, lineNo))
def SavetoExcel(df, xlPath='ConvertedFile.xlsx'):
try:
writer = pd.ExcelWriter(xlPath, engine='xlsxwriter',
date_format='mm/dd/yyy', datetime_format='mm/dd/yyyy')
df.to_excel(writer, index=False)
writer.save()
print('File saved at %s' % xlPath)
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
lineNo = str(exc_tb.tb_lineno)
print('Error : %s : %s at Line %s.' % (type(e), e, lineNo))
if __name__ == '__main__':
Convert2XL('https://www.bls.gov/cew/cewedr10.htm')