Code: Select all
import requests
from bs4 import BeautifulSoup
import pandas as pd
def extract_table_data(url, table_id):
try:
response = requests.get(url,verify=False)
response.raise_for_status()
html_content = response.text
soup = BeautifulSoup(html_content, 'html.parser')
table = soup.find('table', id=table_id)
if not table:
print(f"Table with ID '{table_id}' not found.")
return None
# Extract header row
header_row = [th.get_text(strip=True) for th in table.find_all('th')]
# Extract data rows
data_rows = []
for row in table.find('tbody').find_all('tr'):
data_rows.append([td.get_text(strip=True) for td in row.find_all('td')])
# Create DataFrame
df = pd.DataFrame(data_rows, columns=header_row)
return df
except requests.exceptions.RequestException as e:
print(f"Error during requests: {e}")
return None
except Exception as e:
print(f"An error occurred: {e}")
return None
# Example usage
url = "https://bvmf.bmfbovespa.com.br/clube-de-investimento/clube-de-investimento.aspx?
Idioma=pt-br" # Replace with the actual URL
table_id = "ctl00_contentPlaceHolderConteudo_grdAtivo_ctl01" # Replace with the actual
table ID
table_data = extract_table_data(url, table_id)
if table_data is not None:
print(table_data)