My issue concerns how to access non us-gapp tagged company data in, for example, a 10-q for the ticker MSFT. See this 10q filing: https://www.sec.gov/ix?doc=/Archives/edgar/data/789019/000095017023054855/msft-20230930.htm
Normally, I can just use the following code based on the us-gaap key to get the value:
dep_amort_data = companyFacts.json()['facts']['us-gaap'].get('DepreciationDepletionAndAmortization', {})

However, some stocks, like MSFT, use custom tags for certain values:

When substituting in 'msft' for 'us-gaap'(and the other language) in the previously stated code, no value is found, so a simple key change doesn't work apparently.
dep_amort_data = companyFacts.json()['facts']['msft'].get('DepreciationAmortizationAndOther', {})
I am wondering if anyone knows how to resolve this, because there is clearly a value available per the second image. Perhaps the value is not stored in 'facts'?
Thank you.
Added full method (removed email):
def fetch_data_from_sec_edgar(cik, ticker):
headers = {
'User-Agent': 'EMAIL',
}
# Get company facts data
companyFacts = requests.get(
f'https://data.sec.gov/api/xbrl/companyfacts/CIK{cik}.json',
headers=headers
)
# Check for successful response
if companyFacts.status_code != 200:
print("Failed to fetch company facts")
return 0
# Try to fetch the depreciation and amortization data from the latest 10-Q
try:
if dep_amort_data:
# Filter for 10-Q filings
dep_amort_10q_data = [item for item in dep_amort_data.get('units', {}).get('USD', []) if item['form'] == '10-Q']
if dep_amort_10q_data:
# Find the most recent entry
most_recent_dep_amort = max(dep_amort_10q_data, key=lambda x: x['end'], default={})
return most_recent_dep_amort.get('val', 0)
else:
print("No 10-Q Depreciation and Amortization Data Found")
return 0
else:
tag_ticker = ticker.lower()
dep_amort_data = companyFacts.json()['facts'][tag_ticker].get('DepreciationAmortizationAndOther', {})
if dep_amort_data:
dep_amort_10q_data = [item for item in dep_amort_data.get('units', {}).get('USD', []) if item['form'] == '10-Q']
print(dep_amort_10q_data)
if dep_amort_10q_data:
# Find the most recent entry
most_recent_dep_amort = max(dep_amort_10q_data, key=lambda x: x['end'], default={})
return most_recent_dep_amort.get('val', 0)
else:
print("No 10-Q Depreciation and Amortization Data Found")
return 0
else:
print("No Depreciation and Amortization Data Found")
return 0
except KeyError as e:
print(f"Error fetching depreciation and amortization data: {e}")
return 0
if __name__ == "__main__":
ticker = 'MSFT'
cik = get_cik_from_ticker(ticker)
dep_amort_value = fetch_data_from_sec_edgar(cik, ticker)
print(dep_amort_value)
I solved this issue with custom tags using beautiful soup: