Earnings Announcement Extraction
Is it possible to extract earnings announcements? I am running the below code and it is not coming back correctly for some reason. Thus, I am wondering if it's possible to even do and if so can someone guide me on the below code on how to make it better to fix it? Thank you so much.
from edgar import find from bs4 import BeautifulSoup
class EarningsAnnouncementExtractor:
def __init__(self, ticker):
self.company = find(ticker)
if self.company is None:
raise ValueError(f"Failed to initialize Company object for ticker {ticker}")
self.filings = None
def find_latest_8k_filing(self):
self.filings = self.company.get_filings(form="8-K")
if self.filings is None:
raise ValueError("Failed to retrieve filings.")
latest_filing = self.filings.latest()
return latest_filing
def get_earnings_announcement(self):
latest_filing = self.find_latest_8k_filing()
press_releases = []
for i in range(len(latest_filing.attachments)):
try:
attachment = latest_filing.attachments[i]
if attachment is None:
continue
content = attachment.download()
# Set the encoding explicitly if needed
if isinstance(content, bytes):
content = content.decode('utf-8', errors='replace')
# Parse the HTML content
soup = BeautifulSoup(content, 'html.parser')
# DEBUG: Print out the structure of the document to understand the layout
print(soup.prettify()[:2000]) # Print the first 2000 characters to inspect
# Attempt to find the relevant section using broader search criteria
press_release_section = soup.find_all(string=lambda s: s and "quarter results" in s)
if press_release_section:
for section in press_release_section:
press_release_text = section.find_parent('div').get_text(separator="\n")
press_releases.append(press_release_text)
else:
press_releases.append("Specific earnings announcement section not found.")
except Exception as e:
print(f"Failed to download or process press release for {latest_filing.accession_no}: {e}")
continue
if press_releases:
return press_releases
return "No press release found."
Example Usage for AAPL
try: extractor = EarningsAnnouncementExtractor(ticker="AAPL") earnings_announcements = extractor.get_earnings_announcement() if isinstance(earnings_announcements, list): for idx, announcement in enumerate(earnings_announcements): print(f"Press Release {idx+1}:\n{announcement}\n") else: print(earnings_announcements) except Exception as e: print(f"Error: {e}")
When using this it only provides if an earnings announcement was released or not. I am wondering if there is a way to import/download the actual release and its contents.
from edgar import find from bs4 import BeautifulSoup
class EarningsAnnouncementExtractor:
def __init__(self, ticker):
self.company = find(ticker)
if self.company is None:
raise ValueError(f"Failed to initialize Company object for ticker {ticker}")
self.filings = None
def find_latest_8k_filing(self):
self.filings = self.company.get_filings(form="8-K")
if self.filings is None:
raise ValueError("Failed to retrieve filings.")
latest_filing = self.filings.latest()
return latest_filing
def get_earnings_announcement(self):
latest_filing = self.find_latest_8k_filing()
press_releases = []
for attachment in latest_filing.attachments:
try:
content = attachment.download()
soup = BeautifulSoup(content, 'html.parser')
# Updated: Use 'string' instead of 'text'
press_release_section = soup.find('div', string='Apple reports third quarter results')
if press_release_section:
press_release_text = press_release_section.get_text(separator="\n")
press_releases.append(press_release_text)
else:
press_releases.append("Specific earnings announcement section not found.")
except Exception as e:
print(f"Failed to download or process press release for {latest_filing.accession_no}: {e}")
continue
if press_releases:
return press_releases
return "No press release found."
Example Usage for AAPL
try: extractor = EarningsAnnouncementExtractor(ticker="AAPL") earnings_announcements = extractor.get_earnings_announcement() if isinstance(earnings_announcements, list): for idx, announcement in enumerate(earnings_announcements): print(f"Press Release {idx+1}:\n{announcement}\n") else: print(earnings_announcements) except Exception as e: print(f"Error: {e}")
There is a PressReleases object available on the 8-K filings object. You can access it like so.
filings = get_filings(form="8-K")
eightk = filings[0].obj()
press_releases = eightk.press_releases
press_releases
Thank you for the response. I appreciate it. So just to understand better; is there an actual way to extract the actual earnings announcement once it's posted? Meaning exhibit 99.1. I tried using the snippet you provided here is the script I am using:
from edgar import Company, find
class EarningsAnnouncementExtractor:
def __init__(self, ticker):
self.company = find(ticker)
if self.company is None:
raise ValueError(f"Failed to initialize Company object for ticker {ticker}")
self.filings = None
def find_latest_8k_filing(self):
self.filings = self.company.get_filings(form="8-K")
if not self.filings:
raise ValueError("Failed to retrieve filings.")
latest_filing = self.filings[0].obj()
print("Latest filing found:", latest_filing)
return latest_filing
def get_earnings_announcement(self):
latest_filing = self.find_latest_8k_filing()
try:
attachments = latest_filing.attachments
results = []
for attachment in attachments:
if "EX-99.1" in attachment.filename:
print(f"Processing exhibit: {attachment.filename}")
content = attachment.content.decode(errors='replace') if hasattr(attachment, 'content') else "Content not found."
results.append(content)
print("Final results:", results)
return results
except AttributeError as e:
print(f"Error: {e}")
return []
Example Usage for AAPL
try: extractor = EarningsAnnouncementExtractor(ticker="AAPL")
# Get and print exhibit contents
exhibit_contents = extractor.get_earnings_announcement()
if isinstance(exhibit_contents, list):
for idx, content in enumerate(exhibit_contents):
print(f"Exhibit {idx+1}:\n{content}\n")
else:
print(exhibit_contents)
except Exception as e: print(f"Error: {e}");
I am receiving this output:
Latest filing found: ╭─────────────────────── Apple Inc. 8-K August 01, 2024 ───────────────────────╮ │ Item 2.02 Results of Operations and Financial Condition. │ │ On August 1, 2024, Apple Inc. (“Apple”) issued a press release regarding │ │ Apple’s financial results for its third fiscal quarter ended June 29, 2024. │ │ A copy of Apple’s press release is attached hereto as Exhibit 99.1. │ │ The information contained in this Current Report shall not be deemed “filed” │ │ for purposes of Section 18 of the Securities Exchange Act of 1934, as │ │ amended (the “Exchange Act”), or incorporated by reference in any filing │ │ under the Securities Act of 1933, as amended, or the Exchange Act, except as │ │ shall be expressly set forth by specific reference in such a filing. │ │ │ │ Item 9.01 Financial Statements and Exhibits. │ │ (d)Exhibits. │ │ │ │ ExhibitNumber | Exhibit Description │ │ 99.1 | Press release issued by Apple Inc. on August 1, 2024. │ │ 104 | Inline XBRL for the cover page of this Current Report on Form 8-K. │ │ │ │ │ ╰──────────────────────────────────────────────────────────────────────────────╯
Error: 'EightK' object has no attribute 'attachments'
If you need the text of the earnings announcements do the following
eightk = filing.obj()
eightk['2.02']
Item 2.02 Results of Operations and Financial Condition.
On August 1, 2024, Apple Inc. (“Apple”) issued a press release regarding Apple’s financial results
for its third fiscal quarter ended June 29, 2024.
A copy of Apple’s press release is attached hereto as Exhibit 99.1.
The information contained in this Current Report shall not be deemed “filed” for purposes of Section 18
of the Securities Exchange Act of 1934, as amended (the “Exchange Act”), or incorporated
by reference in any filing under the Securities Act of 1933, as amended, or the Exchange Act,
except as shall be expressly set forth by specific reference in such a filing.
Hey Dwight thank you for your response. So based on your logic I put in both 2.02 and I also used 99.1 which is the actual text of the earrings announcement and the output is now saying: Earnings Announcement Text Found Unexpected format for the earnings announcement. Why do you think this is?
Latest filing found: ╭─────────────────────── Apple Inc. 8-K August 01, 2024 ───────────────────────╮ │ Item 2.02 Results of Operations and Financial Condition. │ │ On August 1, 2024, Apple Inc. (“Apple”) issued a press release regarding │ │ Apple’s financial results for its third fiscal quarter ended June 29, 2024. │ │ A copy of Apple’s press release is attached hereto as Exhibit 99.1. │ │ The information contained in this Current Report shall not be deemed “filed” │ │ for purposes of Section 18 of the Securities Exchange Act of 1934, as │ │ amended (the “Exchange Act”), or incorporated by reference in any filing │ │ under the Securities Act of 1933, as amended, or the Exchange Act, except as │ │ shall be expressly set forth by specific reference in such a filing. │ │ │ │ Item 9.01 Financial Statements and Exhibits. │ │ (d)Exhibits. │ │ │ │ ExhibitNumber | Exhibit Description │ │ 99.1 | Press release issued by Apple Inc. on August 1, 2024. │ │ 104 | Inline XBRL for the cover page of this Current Report on Form 8-K. │ │ │ │ │ ╰──────────────────────────────────────────────────────────────────────────────╯
Earnings Announcement Text Found Unexpected format for the earnings announcement.
from edgar import Company, find
class EarningsAnnouncementExtractor:
def __init__(self, ticker):
self.company = find(ticker)
if self.company is None:
raise ValueError(f"Failed to initialize Company object for ticker {ticker}")
self.filings = None
def find_latest_8k_filing(self):
self.filings = self.company.get_filings(form="8-K")
if not self.filings:
raise ValueError("Failed to retrieve filings.")
latest_filing = self.filings[0].obj()
print("Latest filing found:", latest_filing)
return latest_filing
def get_earnings_announcement(self):
latest_filing = self.find_latest_8k_filing()
try:
# Accessing the specific section of the 8-K filing for earnings announcement
earnings_announcement = latest_filing['2.02']
print("Earnings Announcement Text Found")
return earnings_announcement
except KeyError as e:
print(f"Error: {e}")
return "Earnings announcement section not found."
except AttributeError as e:
print(f"Error: {e}")
return "Attachment content not found."
Example Usage for AAPL
try: extractor = EarningsAnnouncementExtractor(ticker="AAPL")
# Get and print exhibit contents
exhibit_contents = extractor.get_earnings_announcement()
if isinstance(exhibit_contents, dict):
# If the content is in a dictionary format, extract and print it
for key, content in exhibit_contents.items():
print(f"{key}:\n{content}\n")
elif isinstance(exhibit_contents, str):
print(exhibit_contents)
else:
print("Unexpected format for the earnings announcement.")
except Exception as e: print(f"Error: {e}")
See notebook https://github.com/dgunning/edgartools/blob/main/notebooks/Extract-Earnings-Releases.ipynb