Scraping einer dynamischen Datentabelle ohne einfache ReferenzenPython

Python-Programme
Guest
 Scraping einer dynamischen Datentabelle ohne einfache Referenzen

Post by Guest »

Ich versuche, die Daten aus einer einfachen Tabelle von der folgenden Website abzurufen (https://bvmf.bmfbovespa.com.br/clube-de ... Idioma=pt- br). Ich konnte die Daten von der ersten Seite abrufen, aber wie wir sehen können, ist die Paginierung nicht mit der URL verknüpft und ich konnte sie nicht abrufen, obwohl ich unten auf der Seite die Schaltflächen „ProximoPaginacao“ und „ProximoPaginacao“ finden konnte „MeioPaginacao“, aber ich konnte mit dieser Implementierung nicht umgehen. Irgendwelche Ideen?

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)

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post