如何在Intrinio中基于指定标签获取全量企业数据并导出至CSV列
It sounds like you already have a base CSV of full company details (with unique identifiers like company_id or ticker), but need to enrich it with specific tag values (apturnover, 52_week_high, capex, ceo, address) as dedicated columns. Here's a practical, step-by-step solution using Python—perfect for automating data fetching and merging:
Step 1: Prep Your Base Company Data
First, load your existing full company CSV into a dataframe to isolate the unique identifiers you’ll use to fetch tag data. Let’s assume your CSV has a company_id column (swap with ticker if that’s your preferred identifier):
import pandas as pd # Load your base company details file company_df = pd.read_csv('your_full_company_details.csv') # Extract unique company IDs to target for tag data company_ids = company_df['company_id'].unique().tolist()
Step 2: Fetch Tag Data via Intrinio API
You’ll need your Intrinio API key (found in your account settings). We’ll loop through each target tag and company to pull the relevant data, then structure it for easy merging:
import requests INTRINIO_API_KEY = 'your_api_key_here' BASE_URL = 'https://api.intrinio.com' TARGET_TAGS = ['apturnover', '52_week_high', 'capex', 'ceo', 'address'] # Initialize a dictionary to store tag values per company tag_data = {company_id: {} for company_id in company_ids} # Fetch each tag for every company for tag in TARGET_TAGS: for company_id in company_ids: try: # Call Intrinio's API to get the tag value for the company response = requests.get( f"{BASE_URL}/companies/{company_id}/data_point/{tag}", params={'api_key': INTRINIO_API_KEY} ) response.raise_for_status() # Trigger error for HTTP issues data = response.json() tag_data[company_id][tag] = data['value'] except Exception as e: print(f"Failed to fetch {tag} for company {company_id}: {str(e)}") tag_data[company_id][tag] = None # Mark missing data for later cleaning # Convert the tag data dictionary to a dataframe tag_df = pd.DataFrame.from_dict(tag_data, orient='index').reset_index().rename(columns={'index': 'company_id'})
Step 3: Merge and Export the Final CSV
Combine your original company details with the fetched tag data, then export to a new CSV with all your desired columns:
# Merge the two dataframes using company_id as the key final_df = pd.merge(company_df, tag_df, on='company_id', how='left') # Export the enriched data to CSV final_df.to_csv('enriched_company_data.csv', index=False)
Key Tips to Avoid Headaches:
- Rate Limits: Intrinio enforces API rate limits—check your plan and add small delays (
time.sleep(1)) between requests if you’re working with a large number of companies. - Identifier Adjustments: If you use tickers instead of
company_id, update the API endpoint to/securities/{ticker}/data_point/{tag}. - Special Cases: Some tags like
addressmight live under the company profile endpoint (/companies/{company_id}) instead of a data point—adjust the fetch logic if you hit missing data for these fields. - Missing Data: The script marks unavailable tag values as
None(which becomesNaNin the CSV), making it easy to clean or filter later.
内容的提问来源于stack exchange,提问作者griez007

