Advertisement
elena1234

how to scrape a html table in Python

Nov 9th, 2022 (edited)
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | Source Code | 0 0
  1. df = pd.read_html('https://www.nfl.com/standings/league/2022/REG')
  2. df[0].to_excel('output.xlsx', index=False)
  3.  
  4.  
  5. # or
  6. df_first = pd.read_html('https://www.nfl.com/standings/league/2022/REG')[0]
  7.  
  8.  
  9. # or
  10. table_headers = table.findAll('th')
  11. new_columns = [th.text for th in table_headers]
  12. df = pd.DataFrame(columns = new_columns)
  13.  
  14. table_rows = table.findAll('tr')[1:]
  15. for i in table_rows:
  16.     row_data = i.findAll('td')
  17.     row = [cell.text for cell in row_data]
  18.     length = len(df)
  19.     df.loc[length] = row
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement