Downloading Data from the SEC Website using Python
In this blog post, I’ll show you how to download a JSON file from the U.S. Securities and Exchange Commission (SEC) website using Python. The file contains company tickers, which can be useful for various financial analyses and applications.
Steps to Download the File
Here’s a complete Python script that handles the download:
import requests
# URL of the file to download
url = 'https://www.sec.gov/files/company_tickers.json'
# Create a session object
session = requests.Session()
# Define headers to include a User-Agent
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
'Accept': 'application/json',
'Connection': 'keep-alive',
}
# Send a GET request to the URL with headers
response = session.get(url, headers=headers)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Save the content to a local file
with open('company_tickers.json', 'wb') as file:
file.write(response.content)
print("File downloaded successfully!")
else:
print(f"Failed to download file. Status code: {response.status_code}")
Run the Script
Save the script as download_sec_tickers.py
and run it. If everything is set up correctly, you should see the message “File downloaded successfully!” and find the company_tickers.json
file in your current directory.
Conclusion
In this post, we demonstrated how to download a file from the SEC website using Python’s requests
library. This approach can be adapted for downloading other resources from various websites, provided you handle headers and potential restrictions appropriately.