我试图从SEC埃德加数据库中检索数据,我得到了一个有效的端点URL,例如:https://data.sec.gov/submissions/CIK0000320193.json(这是Apple Inc的端点),但当我试图将结果存储在变量中时,我得到了一个空列表:results = [],当我将结果保存在txt文件中时.
def get_sec_data(cik):
try:
# Define the SEC data.gov API endpoint for fetching filings in JSON format
endpoint = f"https://data.sec.gov/submissions/CIK{cik}.json"
# Print the API endpoint
print("API Endpoint:", endpoint)
# Define the headers with the required User-Agent
headers = {
'User-Agent': "dom dom [email protected]",
'Accept-Encoding': 'gzip, deflate'
}
# Make a GET request to the SEC data.gov API with the specified headers
response = requests.get(endpoint, headers=headers)
# Check if the request was successful (status code 200)
if response.status_code != 200:
raise Exception(f"Failed to fetch SEC data. Status: {response.status_code}")
# Parse the JSON response
json_data = response.json()
# Extract the XBRL data for the latest 10-K and 10-Q filings
filings_data = json_data.get('hits', [])
return filings_data
except Exception as e:
raise Exception(f"Error: {str(e)}")
字符串
然后我得到结果并试图保存它
# Retrieve the last 10-K and 10-Q filings for the specified company in JSON format
try:
results = get_sec_data(cik)
# Display the results or handle errors
for result in results:
if "Error" in result:
print(result)
else:
print("Successfully retrieved data from SEC data.gov API.")
# You can now use the variable 'results' for further processing.
# Specify the file name without a path
file_name = "test.txt"
# Construct the full file path by joining it with the current working directory
file_path = "/users/domdom/desktop/Project/" + file_name
# Open the file in write mode
with open(file_path, 'w') as file:
# Iterate over the results and write each entry to the file
for entry in results:
file.write(str(entry) + '\n')
print(f"Data has been written to {file_path}")
except Exception as e:
print(e)
型
我错了什么?我尝试了不同的检查,看看我获得的端点链接是否正确,如果我从SEC网站检索数据,当我在浏览器中打开链接时,它都能正常工作,我还尝试保存CSV而不是txt,同样的事情:空。似乎问题在于存储,然后从端点保存内容。
1条答案
按热度按时间1mrurvl11#
你想在这条线上做什么:
filings_data = json_data.get('hits', [])
?看起来你试图从埃德加返回的JSON数据中获取属性
hits
的值,但据我所知没有这样的属性。尝试获取不同的属性(例如tickers
),你应该会得到更好的结果。