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

87
March 28, 2022, at 3:40 PM

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)
Answer 1

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")
Answer 2

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

READ ALSO
Replace other values with np.nan

Replace other values with np.nan

I have a pandas data frame:

87
Python, calculator class definition

Python, calculator class definition

I'm struggling with a practice problem below where I'm confused as to what the definitions would look like

98
python pydantic How to choose class based on another value

python pydantic How to choose class based on another value

some of example of schemaI have different constraints based on type

106
How to fit UMAP object with an embedding_ property of shape (200,2)

How to fit UMAP object with an embedding_ property of shape (200,2)

I'm trying to take the first 200 rows of my dataframe and pass it through a UMAP fit method but I'm not able to get it to shape the way I want at (200,2)Should I add an embedding parameter?

77