10

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

1 Answer 1

20

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]
Sign up to request clarification or add additional context in comments.

1 Comment

above code is working fine , i was looking for writing a for loop for every column and then further cbinding it . i usually code in R , Pardon me i am new to python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.