Is there a way to return one specific table from a webpage that has multiple tables in Python?

Viewed 48

I'm having trouble returning one particular table (the one titled 'BRN Substantial Shareholders') from this webpage - https://www.intelligentinvestor.com.au/shares/asx-brn/brainchip-holdings-ltd

I'm able to return all the tables using the below code.

html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
all_tables = soup.find_all('table')

I've tried two different methods to try and scrape using bs but I can't seem to find a way - am I doing something wrong? Both of these output an empty list.

Method 1

# Scrape the substantial holder list
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')

sub_headers = []
sub_holdings = []

for found_table in soup.find_all('table', class_=f'{ticker_code} + "Substantial Shareholders"'):
    sub_headers = found_table.find_all('th').append(sub_headers)
    sub_holdings = found_table.find_all('td').append(sub_holdings)

print(sub_headers)
print(sub_holdings)

Method 2

# Scrape the substantial holder list
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')

all_headers = soup.find_all("th", class_=f"{ticker_code} Substantial Shareholders")
all_holdings = soup.find_all("tr", class_=f"{ticker_code} Substantial Shareholders")

sub_headers = []
sub_holdings = []

for header in all_headers:
    sub_headers.append(header.text)

for holding in all_holdings:
    holding.append(sub_holdings.text)

print(sub_headers)
print(sub_holdings)
2 Answers

To only scrape the table with the words "BRN Substantial Shareholders", you can use a CSS selector to locate that table with:

table = soup.select_one("div:nth-of-type(11) table")

Found a much simpler method using below. sub_table = pd.read_html(current_url, match='Holding') print(sub_table)

Related