Skip to content

Instantly share code, notes, and snippets.

@n8henrie
Created January 22, 2015 00:37
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n8henrie/08a31f02fd1282d12b75 to your computer and use it in GitHub Desktop.
Save n8henrie/08a31f02fd1282d12b75 to your computer and use it in GitHub Desktop.
Prompts for a URL, displays HTML tables from that page, then converts the selected table to a csv file.
#! /usr/bin/env python3
"""html_to_csv.py
Prompts for a URL, displays HTML tables from that page, then converts
the selected table to a csv file.
"""
import sys
import pandas
url = input("Enter the URL: ")
tables = pandas.io.html.read_html(url)
for index, table in enumerate(tables):
print("Table {}:".format(index + 1))
print(table.head() + '\n')
print('-' * 60)
print('\n')
choice = int(input("Enter the number of the table you want: ")) - 1
filename = input("Enter a filename (.csv extension assumed): ") + '.csv'
with open(filename, 'w') as outfile:
tables[choice].to_csv(outfile, index=False, header=False)
@aletinjs
Copy link

Hello. Why I keep gettinn an error
File "./file.py", line 14
print("Table {}:".format(index + 1))
^
IndentationError: expected an indented block

@n8henrie
Copy link
Author

Are you sure you're copying the code correctly? Looks like you're missing the 4 spaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment