让 Agent 读取网页内容
This content is not available in your language yet.
这一页讲的是一个非常具体的场景:
当 Agent 需要读网页,而模型本身又拿不到实时网页内容时,如何补一个“网页读取工具”。
Jina AI 提供了一个很直接的能力:把网页 URL 转成结构化文本。
对于 Agent 来说,这种能力刚好适合被包装成工具。
什么时候值得这么做
Section titled “什么时候值得这么做”- 问题依赖网页上的实时信息
- 你不想自己维护复杂的网页抓取逻辑
- 你只需要“读网页正文”,而不是完整浏览器渲染能力
如果你的任务根本不需要联网,或者只依赖本地知识库,就没必要把这页的方案接进来。
思路其实很简单:
- 实现一个
AgentTool - 工具里接收 URL 参数
- 调用 Jina AI 的网页读取接口
- 把返回文本交回 Agent
最小工具实现
Section titled “最小工具实现”import com.alibaba.fastjson2.JSONObject;import tech.smartboot.feat.Feat;import tech.smartboot.feat.ai.agent.AgentTool;import tech.smartboot.feat.core.common.FeatUtils;
import java.util.concurrent.CompletableFuture;
public class JinaReaderTool implements AgentTool {
@Override public CompletableFuture<String> execute(JSONObject parameters) { String url = parameters.getString("url"); if (FeatUtils.isBlank(url)) { return CompletableFuture.completedFuture("错误:必须提供 url 参数"); }
String jinaUrl = buildJinaUrl(url);
return Feat.httpClient(jinaUrl, opts -> opts.debug(false)) .get() .submit() .thenApply(response -> response.body()) .exceptionally(e -> "执行失败: " + e.getMessage()); }
private String buildJinaUrl(String targetUrl) { if (targetUrl.startsWith("http://")) { return "https://r.jina.ai/http://" + targetUrl.substring(7); } else if (targetUrl.startsWith("https://")) { return "https://r.jina.ai/https://" + targetUrl.substring(8); } return "https://r.jina.ai/https://" + targetUrl; }
@Override public String getName() { return "jina_web_reader"; }
@Override public String getDescription() { return "读取任意网页内容并返回结构化文本"; }
@Override public String getParametersSchema() { return "{\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"url\": {\n" + " \"type\": \"string\",\n" + " \"description\": \"要读取的网页 URL\"\n" + " }\n" + " },\n" + " \"required\": [\"url\"]\n" + "}"; }}挂到 Agent 上
Section titled “挂到 Agent 上”FeatAgent agent = FeatAI.agent(opts -> { opts.chatOptions().model(ChatModelVendor.GiteeAI.Qwen2_5_72B_Instruct); opts.tool(new JinaReaderTool()); opts.maxIterations(10);});
String result = agent.execute( "查看 https://jina.ai 并告诉我这是做什么的公司").get();这就够了。
Agent 现在已经拥有了一个“读取网页正文”的工具能力。
什么时候它有价值
Section titled “什么时候它有价值”这个工具最适合拿来做:
- 新闻摘要
- 产品调研
- 文档检索
- 实时网页内容分析
它不适合解决:
- 需要完整浏览器执行 JavaScript 的场景
- 需要登录态或复杂反爬处理的页面
- 高频、大规模抓取任务
一个实践建议
Section titled “一个实践建议”第一次接这类工具时,不要一口气加太多外部工具。
先只给 Agent 一个网页读取工具,看它能不能稳定完成任务,再决定要不要继续加搜索、文件读写等能力。
- 如果你还没真正用过 Agent,先回到 让模型开始使用工具
- 如果你只是想先把模型本身跑熟,回到 配置对话模型并处理流式输出