- searches the web for the latest updates on a list of companies,
- summarizes one interesting story for each company,
- analyzes the sentiment of that summary (positive / neutral / negative),
- exports the aggregated results into a clean Excel file for further review.
Stock News Pro. Save it and make sure your environment contains your OpenAI API key (for example, in a .env file).
Make sure your
.env contains a valid API key (for example OPENAI_API_KEY=...). This lesson uses an agents package that provides an Agents SDK and a WebSearchTool to perform live web queries.Overview
This guide is organized into four clear steps:- Setup and imports
- Main logic (single async function that performs search, summarize, classify, and collect)
- Running the script (script vs. notebook)
- Output format and where files are saved
1 โ Setup and imports
Load environment variables and import the core libraries:2 โ Main logic
Below is a single consolidated async function that:- Creates an
Agentwith theWebSearchTool. - For each stock:
- searches the web and requests one recent update in a single sentence,
- extracts the summary,
- asks the agent to classify sentiment (positive / neutral / negative),
- maps the sentiment to an emoji,
- extracts a source link if available,
- appends the result to the
resultslist.
- Converts the list to a pandas
DataFrameand exports it to Excel (both the current working directory and the userโs Downloads folder when possible).
The script performs live web searches using the
WebSearchTool. Expect variability in outputs and occasional missing source links. Monitor API usage and rate limits for your API key to avoid unexpected charges.3 โ Running the script
If you are running this as a standalone script, start the async function like this:4 โ What the output looks like
When the script finishes you will see the Excel files (if both saves succeeded):| Location | Filename |
|---|---|
| Current working directory | Stock_News_Summary_Pro.xlsx |
| Userโs Downloads folder | Stock_News_Summary_Pro.xlsx |
| Column | Description | Example |
|---|---|---|
| Stock | The company name being tracked | Apple |
| News | The one-sentence recent update returned by the agent | Apple announces new AI features in iOS. |
| Sentiment | Sentiment label with emoji (e.g., โPositive โ โ) | Positive โ
|
| Link | Source URL if available (None or blank if not found) | https://example.com/news/article |
Links and references
- OpenAI API keys
- Pandas documentation: https://pandas.pydata.org/docs/
- Python asyncio: https://docs.python.org/3/library/asyncio.html