Access Denied (103) 【教程】用Python+Agent-Reach零成本构建AI信息搜集助手 - 技能分享 - 闲社 - Powered by Discuz! Archiver

bibylove 发表于 6 天前

【教程】用Python+Agent-Reach零成本构建AI信息搜集助手

【教程】用Python+Agent-Reach零成本构建AI信息搜集助手

最近在GitHub Trending上看到一个非常实用的开源项目——Agent-Reach,它能让你的AI Agent拥有"眼睛",零API费用就能搜索Twitter、Reddit、YouTube、GitHub、B站、小红书等平台。今天手把手教大家从0到1搭建一个AI信息搜集助手。



一、前置条件

开始之前,确保环境满足:

[*]Python 3.9+(推荐3.10或3.11)
[*]pip包管理工具
[*]Git用于克隆仓库
[*]基础Python编程知识

验证Python版本:
python --version
# 输出应 >= 3.9

如果版本不够,建议用pyenv或conda管理。



二、安装Agent-Reach

安装非常简单,支持pip直接安装:

# 创建虚拟环境(推荐)
python -m venv venv
source venv/bin/activate

# 安装Agent-Reach
pip install agent-reach

# 验证
agent-reach --version

也可以从源码安装:
git clone https://github.com/Panniantong/Agent-Reach.git
cd Agent-Reach
pip install -e .

底层使用playwright,如安装失败可手动执行:
playwright install



三、核心功能快速上手

Agent-Reach的核心设计是"一个CLI,零API费用"。

1. 搜索Twitter/X
from agent_reach import TwitterReach

twitter = TwitterReach()
results = twitter.search("AI Agent 2026", limit=10)

for tweet in results:
    print(f"作者: {tweet.author}")
    print(f"内容: {tweet.text[:100]}...")
    print(f"时间: {tweet.created_at}")

2. 抓取Reddit讨论
from agent_reach import RedditReach

reddit = RedditReach()
posts = reddit.get_hot("MachineLearning", limit=5)

for post in posts:
    print(f"标题: {post.title}")
    print(f"点赞: {post.score}")
    print(f"评论: {post.num_comments}")

3. 搜索B站视频
from agent_reach import BilibiliReach

bili = BilibiliReach()
videos = bili.search("Python教程", limit=5)

for video in videos:
    print(f"标题: {video.title}")
    print(f"UP主: {video.author}")
    print(f"播放量: {video.play_count}")

4. 小红书笔记搜索
from agent_reach import XiaohongshuReach

xhs = XiaohongshuReach()
notes = xhs.search("AI工具推荐", limit=5)

for note in notes:
    print(f"标题: {note.title}")
    print(f"点赞: {note.likes}")

5. GitHub仓库搜索
from agent_reach import GitHubReach

gh = GitHubReach()
repos = gh.search_repos("AI agent python", sort="stars", limit=10)

for repo in repos:
    print(f"仓库: {repo.full_name}")
    print(f"星标: {repo.stars}")
    print(f"描述: {repo.description}")

以上所有操作不需要任何API Key,完全免费。



四、实战:构建AI资讯聚合器

下面做一个完整项目:每天自动搜集AI行业动态,生成结构化报告。

#!/usr/bin/env python3
import json
from datetime import datetime
from agent_reach import TwitterReach, RedditReach, GitHubReach

class AINewsAggregator:
    def __init__(self):
      self.twitter = TwitterReach()
      self.reddit = RedditReach()
      self.github = GitHubReach()
      self.results = {
            "twitter": [],
            "reddit": [],
            "github": [],
            "generated_at": datetime.now().isoformat()
      }
   
    def collect_twitter(self, keyword="AI news", limit=5):
      print(f" 搜索Twitter: {keyword}")
      tweets = self.twitter.search(keyword, limit=limit)
      for tweet in tweets:
            self.results["twitter"].append({
                "author": tweet.author,
                "text": tweet.text,
                "url": tweet.url
            })
      print(f"    ✓ 获取 {len(self.results['twitter'])} 条")
   
    def collect_reddit(self, subreddit="MachineLearning", limit=5):
      print(f" 获取Reddit: r/{subreddit}")
      posts = self.reddit.get_hot(subreddit, limit=limit)
      for post in posts:
            self.results["reddit"].append({
                "title": post.title,
                "score": post.score,
                "url": post.url
            })
      print(f"    ✓ 获取 {len(self.results['reddit'])} 条")
   
    def collect_github(self, keyword="AI agent", limit=5):
      print(f" 搜索GitHub: {keyword}")
      repos = self.github.search_repos(keyword, sort="stars", limit=limit)
      for repo in repos:
            self.results["github"].append({
                "name": repo.full_name,
                "stars": repo.stars,
                "description": repo.description
            })
      print(f"    ✓ 获取 {len(self.results['github'])} 个")
   
    def generate_report(self, output_file="ai_news_report.json"):
      with open(output_file, "w", encoding="utf-8") as f:
            json.dump(self.results, f, ensure_ascii=False, indent=2)
      print(f"\n报告已保存: {output_file}")
   
    def run(self):
      print("=== AI资讯聚合器 ===\n")
      self.collect_twitter()
      self.collect_reddit()
      self.collect_github()
      self.generate_report()

if __name__ == "__main__":
    AINewsAggregator().run()

运行效果:
=== AI资讯聚合器 ===

搜索Twitter: AI news
    ✓ 获取 5 条
获取Reddit: r/MachineLearning
    ✓ 获取 5 条
搜索GitHub: AI agent
    ✓ 获取 5 个

报告已保存: ai_news_report.json

生成的JSON可直接导入数据库、发送邮件或接入其他系统。



五、常见问题

Q1: 报错 "playwright not found"?
A: 执行:
playwright install chromium

Q2: Twitter访问受限?
A: 可配置代理或降低请求频率。

Q3: 如何接入AI模型?
A: 将搜集结果传给任何LLM分析:
import openai

with open("ai_news_report.json", "r") as f:
    data = json.load(f)

prompt = f"基于以下动态生成中文摘要...
{json.dumps(data, ensure_ascii=False)}"

response = openai.ChatCompletion.create(
    model="gpt-4",
    messages=[{"role": "user", "content": prompt}]
)

Q4: 如何定时执行?
A: 用cron定时任务:
0 9 * * * cd /项目路径 && python ai_news_aggregator.py

Q5: 支持哪些平台?
A: Twitter/X、Reddit、YouTube、GitHub、Bilibili、小红书,持续增加中。



六、总结

Agent-Reach解决了AI开发的核心痛点:让AI获取实时信息,无需昂贵API费用。

今天学会了:

[*]安装配置Agent-Reach
[*]调用各平台搜索功能
[*]构建AI资讯聚合器
[*]将结果接入AI模型分析

适用场景:

[*]个人开发者给AI项目加实时信息能力
[*]小型团队低成本信息监控
[*]学习AI Agent开发的入门项目
[*]多平台内容聚合

GitHub地址:https://github.com/Panniantong/Agent-Reach
星标已突破2.9万,社区活跃,有问题可提Issue。

互动话题:
1. 你平时用什么方式给AI补充实时信息?
2. 还希望Agent-Reach支持哪些平台?
3. 你会用这个工具做什么项目?

欢迎评论分享!有帮助记得点赞收藏~
页: [1]
查看完整版本: 【教程】用Python+Agent-Reach零成本构建AI信息搜集助手