I have been trying to scrape a table from this website https://www.alphaquery.com/stock/aapl/earnings-history
but in no way i can achieve it. i can t even find the table.
import requests
from bs4 import BeautifulSoup
def get_eps(ticker):
url = f"https://www.alphaquery.com/stock/{ticker}/earnings-history"
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
# Attempt to more robustly find the table by checking for specific headers
table = None
for table_candidate in soup.find_all("table"):
headers = [th.get_text(strip=True) for th in table_candidate.find_all("th")]
if "Estimated EPS" in headers:
table = table_candidate
break
if table:
rows = table.find_all('tr')[1:6]
for row in rows:
cells = row.find_all('td')
if len(cells) >= 4: # Ensure there are enough columns in the row
try:
est_eps = cells[2].text.strip().replace('$', '').replace(',', '')
except ValueError:
continue # Skip rows where conversion from string to float fails
else:
print(f"Failed to find earnings table for {ticker}")
return est_eps
# Example usage
ticker = 'AAPL'
beats = get_eps(ticker)
print(f'{ticker} estimates {est_eps}')