Nabi Shaikh
Nabi Shaikh

Reputation: 851

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

Upvotes: 10

Views: 23322

Answers (1)

Prasun Chakraborty
Prasun Chakraborty

Reputation: 547

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]

Upvotes: 20

Related Questions