利用人工智能分析创新雅虎财经
我建造了什么
我开发了一个项目,该项目会抓取 Yahoo Finance 以收集最新的财经新闻和世界股票指数。抓取的数据通过 OpenAI API 使用 AI 进行分析,提供趋势摘要和情绪分析等见解。然后可以将结果自动发送到 Telegram 机器人频道以方便访问。
抓取工具支持调度,允许它定期运行(例如每小时)或在需要时立即执行。
通过动态向下滚动来加载附加内容,可以抓取新闻。然后,在新的浏览器选项卡中打开每个故事以提取其完整内容。默认情况下,如 `.env` 文件中的 `YAHOO_FINANCE_NEWS_PAGES_LIMIT` 所定义,仅处理第一页(包含最近的 10 个故事)以保持聚合消息高效发送到 OpenAI,否则不会达到限制。对于自定义,您可以增加最大页面限制,选择不同的 OpenAI 模型,或调整令牌限制 `OPENAI_MAX_TOKENS` 以分析更广泛的结果。
抓取工具可以连接到 Bright Data Scraping Browser,以满足高级抓取需求,例如解析 CAPTCHA 或绕过 IP 阻止。在配置文件中设置“BROWSER_WS”后,抓取工具将连接到远程浏览器实例。
配置
可以使用覆盖“.env”文件的“.env.local”文件来配置项目。
默认项目配置存在于 `.env` 文件中:
# WebSocket endpoint for a remote browser instance. # If empty, Puppeteer will launch a local browser instance. BROWSER_WS= # Cron schedule for running the scraper. # Default: Every hour. Leave empty to run the scraper immediately upon starting. SCHEDULE='0 * * * *' # Notification channels for sending alerts or reports. # Supported values: 'telegram'. Use a comma-separated list for multiple channels. NOTIFICATION_CHANNELS= # Enable or disable AI analysis of scraping results. # Set to 'true', '1' to enable, or leave empty to disable AI analysis. OPENAI_ENABLED=false # OpenAI API key for accessing AI services. # Obtain your API key from https://platform.openai.com/account/api-keys OPENAI_API_KEY= # OpenAI model to use for analysis. # Use 'gpt-3.5-turbo' for cost-efficiency or 'gpt-4o' for more advanced analysis. OPENAI_MODEL=gpt-3.5-turbo # Maximum number of tokens for AI responses. # Increase this value for longer analyses, keeping in mind token limits for your selected model. OPENAI_MAX_TOKENS=1000 # Instruction for guiding the AI behavior during analysis. # Customize this text to match your use case or desired output format. OPENAI_INSTRUCTION='You are a financial analyst AI. Analyze the following financial news data, summarize trends, and highlight key positive or negative sentiments about the stock market.' # Telegram bot token for sending notifications. # Obtain this token from BotFather in Telegram. TELEGRAM_BOT_TOKEN= # Telegram chat ID for sending messages. # Use the chat ID of the recipient (user or group) where notifications should be sent. TELEGRAM_CHAT_ID= # Maximum number of news pages to scrape from Yahoo Finance. # This limits how many pages of news will be processed during each scraping run. # Example: # - Set to 1 to scrape only the first page of news. # - Set to 5 to scrape up to 5 pages of news (if available). YAHOO_FINANCE_NEWS_PAGES_LIMIT=1
如何运行抓取工具
您需要安装所需的节点模块来设置和运行抓取工具。虽然我建议使用“pnpm”包管理器,但您也可以根据需要使用“npm”或“yarn”。
安装
运行以下命令来安装必要的软件包:
pnpm install
开始
要启动抓取工具,您需要使用默认配置执行下一个命令:
pnpm start
日志和调试
当抓取工具运行时,它会将详细的日志消息输出到控制台。
这些日志包括:
GitHub Repo:链接
如何使用 Bright Data
使用 BrightData Scrapping Browser 在处理复杂场景方面具有显著优势,例如解决 CAPTCHA、绕过 IP 阻止,因为频繁访问 Yahoo 可能会触发 IP 禁令和 CAPTCHA 挑战。此外,同意表单仅在本地浏览器实例中打开,Bright Data Scrapping Browser 可以处理它。但是,它有一些权衡:

为了应对这些挑战,我实施了一个重试系统来自动处理连接中断和远程浏览器的意外问题。然而,在某些情况下,即使这样也不足以确保完美运行。
结论
该项目展示了将先进的网页抓取技术与 AI 分析相结合的潜力,可以从 Yahoo Finance 等动态且复杂的网站中提取有意义的见解。使用 Bright Data Scraping Browser 等工具,抓取工具可以处理诸如 CAPTCHA 解析和 IP 阻止等挑战,从而非常有效地完成大规模且复杂的抓取任务。
Bright Data 显著增强了抓取功能,但也带来了复杂性,例如调试远程实例和管理稳定性。
OpenAI 通过将原始抓取数据转换为可操作的摘要、趋势和情绪分析来增加价值。此功能与灵活的调度和 Telegram 通知相结合,使该项目成为监控金融新闻和股票指数的多功能工具。
最终,本文展示了现代抓取工具和人工智能如何协同应对现实世界的数据挑战,为未来更先进、更用户友好的应用程序铺平了道路。
感谢您花时间探索这个项目!
DEV 挑战现已开始!

查看所有参与方式、证明您的技能并赢取奖品。
访问挑战中心