Read table from Web using Python

Viewed 352

I'm new to Python and am working to extract data from website https://www.screener.in/company/ABB/consolidated/ on a particular table (the last table which is Shareholding Pattern)

I'm using BeautifulSoup library for this but I do not know how to go about it.

So far, here below is my code snippet. am failing to pick the right table due to the fact that the page has multiple tables and all tables share common classes and IDs which makes it difficult for me to filter for the one table I want.

import requests import urllib.request
from bs4 import BeautifulSoup
    
url = "https://www.screener.in/company/ABB/consolidated/"

r = requests.get(url)
print(r.status_code)
html_content = r.text
soup = BeautifulSoup(html_content,"html.parser")
# print(soup)
#data_table = soup.find('table', class_ = "data-table")
# print(data_table) table_needed = soup.find("<h2>ShareholdingPattern</h2>")
#sub = table_needed.contents[0] print(table_needed)
1 Answers

Just use requests and pandas. Grab the last table and dump it to a .csv file.

Here's how:

import pandas as pd
import requests

df = pd.read_html(
    requests.get("https://www.screener.in/company/ABB/consolidated/").text,
    flavor="bs4",
)
df[-1].to_csv("last_table.csv", index=False)

Output from a .csv file:

enter image description here

Related