How to Easily Scrape HTML Tables from a Website with Python Pandas

Pandas has a function pd.read_html() to get an HTML table from a website in one line of code.

For this example, I am using the website https://countrycode.org/ which has a simple HTML table with countries and their country codes.

The pandas function pd.read_html() can takes as little as one argument to work. All you need to do is pass in a website and see what happens. If successful, you will get a list of DataFrames that the function was able to scrape. Finally, you can return a single DataFrame by its index position. Pass [0] to get the first DataFrame from the list, for example.

country_codes = pd.read_html("https://countrycode.org/")[0]
country_codes

Final Thoughts

Check out more Python tricks in this Colab Notebook or in my recent Python Posts.

Thanks for reading!


Posted

in

by

Tags: