Как очистить данные таблицы из HTML?Python

Программы на Python
Anonymous
Как очистить данные таблицы из HTML?

Сообщение Anonymous »

Я пытался получить таблицу с этого сайта https://www.alphaquery.com/stock/aapl/earnings-history
но никак не могу этого добиться. Я даже таблицу не могу найти.
import requests
from bs4 import BeautifulSoup

def get_eps(ticker):
url = f"https://www.alphaquery.com/stock/{ticke ... gs-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}')


Подробнее здесь: https://stackoverflow.com/questions/784 ... -from-html

Вернуться в «Python»