Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert HTML Table to Pandas Data Frame in Python

Here i am trying to extract a table from a website as specified in Python code . i am able to get the HTML Table and further i am unable to convert to data frame using Python . Here is the code

# import libraries
import requests
from bs4 import BeautifulSoup

# specify url
url = 'http://my-trade.in/'

# request html
page = requests.get(url)

# Parse html using BeautifulSoup, you can use a different parser like lxml if present
soup = BeautifulSoup(page.content, 'html.parser')

tbl =soup.find("table",{"id":"MainContent_dataGridView1"})

How to convert this HTML Format Table to Data Frame

like image 744
Nabi Shaikh Avatar asked Jul 10 '19 09:07

Nabi Shaikh


People also ask

Can pandas read an HTML table?

Pandas is one of the most popular Python libraries for data analysis. This library has many useful functions. One of such functions is pandas read_html. It can convert HTML tables into pandas DataFrame efficiently.

How read data from HTML file in pandas?

To read an HTML file, pandas dataframe looks for a tag . That tag is called a <td></td> tag. This tag is used for defining a table in HTML. pandas uses read_html() to read the HTML document.

How do you create a DataFrame from a table in Python?

To create a dataframe, we need to import pandas. Dataframe can be created using dataframe() function. The dataframe() takes one or two parameters. The first one is the data which is to be filled in the dataframe table.


1 Answers

You can just Use pandas read_html function for that, and remember to convert the html you get to string else you will get some parsing error.

import requests
from bs4 import BeautifulSoup
import pandas as pd

url = 'http://my-trade.in/'
page = requests.get(url)

soup = BeautifulSoup(page.content, 'html.parser')

tbl = soup.find("table",{"id":"MainContent_dataGridView1"})

data_frame = pd.read_html(str(tbl))[0]
like image 195
Prasun Chakraborty Avatar answered Sep 20 '22 18:09

Prasun Chakraborty