forked from E-Virgil/NLP_Final
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsec_api.py
More file actions
71 lines (57 loc) · 2.58 KB
/
Copy pathsec_api.py
File metadata and controls
71 lines (57 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import json
import requests
import os
def download_10q_reports(ticker):
# Load company tickers from the JSON file
with open('company_tickers.json') as f:
company_data = json.load(f)
# Find the company by ticker
company_info = next((info for info in company_data.values() if info['ticker'] == ticker), None)
if not company_info:
print(f"No data found for ticker: {ticker}")
return
cik = company_info['cik_str']
base_url = "https://data.sec.gov/api/xbrl/"
headers = {
'User-Agent': 'Your Name AdminContact@yourdomain.com', # Change this line
'Accept-Encoding': 'gzip, deflate'
}
# Fetch 10-Q filings
filings_url = f"https://data.sec.gov/submissions/CIK{str(cik).zfill(10)}.json"
response = requests.get(filings_url, headers=headers)
if response.status_code != 200:
print(f"Failed to fetch filings for CIK {cik}: {response.text}")
return
filings_data = response.json()
# Extract recent filings
recent_filings = filings_data['filings']['recent']
# Create a list of filings with relevant data
reports = []
for i in range(len(recent_filings['form'])):
if recent_filings['form'][i] == '10-Q':
print(recent_filings.keys())
report = {
'form': recent_filings['form'][i],
'filingDate': recent_filings['filingDate'][i],
'accessionNumber': recent_filings['accessionNumber'][i],
'fileName': recent_filings['primaryDocument'][i]
}
reports.append(report)
# Download up to 10 reports
for report in reports[:10]:
filing_date = report['filingDate']
accession_number = report['accessionNumber']
report_url = f"https://www.sec.gov/Archives/edgar/data/{cik}/{accession_number.replace('-', '')}/{report['fileName']}"
# Define the download path
download_path = os.path.join(os.getcwd(), f"{ticker}_10Q_{filing_date}.txt")
print(f"Downloading: {report_url} to {download_path}")
report_response = requests.get(report_url, headers=headers)
if report_response.status_code == 200:
with open(download_path, 'wb') as f:
f.write(report_response.content)
print(f"Downloaded: {download_path}")
else:
print(f"Failed to download report: {report_response.text}")
if __name__ == "__main__":
ticker_input = input("Enter the ticker symbol (e.g., AAPL): ").strip()
download_10q_reports(ticker_input)