@browser_use: 我们推出了 Fetch Use,这是用地球上最隐秘的浏览器抓取任何网站的最简单方法。代理、Cookie…

X AI KOLs Following 工具

摘要

Browser Use 推出了 Fetch Use,这是一个 Python SDK,用于通过隐身浏览器抓取网站,自动处理代理、Cookie 和会话。

我们推出了 Fetch Use,这是用地球上最隐秘的浏览器抓取任何网站的最简单方法。 代理、Cookie 和会话全部自动处理,因此您无需担心被屏蔽。 查看我们的博客文章了解更多: https://t.co/Dv1HBFdgup
查看原文
查看缓存全文

缓存时间: 2026/06/11 15:38

我们推出了 Fetch Use,这是抓取任何网站的最简单方法,配合地球上最隐秘的浏览器使用。

代理、Cookie 和会话都已为您处理,因此您无需担心被屏蔽。

更多详情请见我们的博文: https://t.co/Dv1HBFdgup


一个用于抓取任何网站的 Python 程序

来源:https://browser-use.com/posts/fetch-use 你找到了一个包含所需数据的网站。可能是一个产品列表,也可能是一篇文章。你想要一个简单的 Python 脚本,打开页面、提取数据,然后给你一些有用的东西。

一个普通的 Python 请求常常会被屏蔽或重定向,而抓取可能很复杂。这正是 fetch-use 的用途。

图表展示:一个 Python 脚本将 URL 发送给 fetch-use,fetch-use 请求网站,然后将响应以可用内容的形式返回给 Python。

什么是 fetch-use?

https://browser-use.com/posts/fetch-use#what-is-fetch-use

fetch-use 是一个 Python SDK,用于像真实浏览器那样获取网站内容。

这一点很重要,因为你看到的网页往往与你的爬虫看到的页面不同。网站可能会发送不同的 HTML、重定向你,或者完全阻止请求。

使用 fetch-use,你可以提供一个 URL,然后以更易于处理的格式获取页面内容。它处理了网页抓取中所有烦人的部分。

如果你还在简单爬虫和完整浏览器代理之间犹豫不决,请阅读我们的 网页抓取指南

fetch-use 是如何工作的?

https://browser-use.com/posts/fetch-use#how-does-fetch-use-work

当你调用 fetch-use 时,你的脚本会向 Browser Use 发送一个请求。Browser Use 以一种比普通 Python 脚本更接近真实浏览器的方式获取 URL,处理重定向、Cookie、会话和响应格式化等细节,然后将响应返回给你的 Python 程序。

安装 fetch-use

https://browser-use.com/posts/fetch-use#install-fetch-use

安装 SDK:

pip install fetch-use

设置你的 Browser Use API 密钥:

export BROWSER_USE_API_KEY=bu_your_api_key

你可以在 Browser Use Cloud 中创建一个 API 密钥。

你的第一个请求

https://browser-use.com/posts/fetch-use#your-first-request

这是最简单的入门方式:

from fetch_use import fetch_sync

response = fetch_sync("https://example.com", output_format="markdown")

print(response.status_code)
print(response.text)

fetch-use 一个 URL,它就会返回页面内容。

响应对象包含你期望的字段:

print(response.status_code)
print(response.headers)
print(response.text)

如果你调用的是返回 JSON 的 API 端点,可以直接解析响应体:

data = response.json()

使用 Markdown 输出获取可读页面

https://browser-use.com/posts/fetch-use#use-markdown-output-for-readable-pages

原始的 HTML 通常很杂乱。一个页面可能包含导航、脚本、样式、页脚、跟踪标签、布局标记,以及其他与你实际想要的数据无关的内容。

对于文章、文档、产品描述和其他以文本为主的页面,output_format="markdown" 通常更容易处理:

from fetch_use import fetch_sync

response = fetch_sync(
    "https://example.com/blog-post",
    output_format="markdown",
)

print(response.text)

不必立即解析一个庞大的 HTML 文档,你可以从更干净的文本开始。

使用结构化输出来检查页面

https://browser-use.com/posts/fetch-use#use-structured-output-to-inspect-a-page

当你第一次探索一个页面时,你可能还不知道有哪些内容可用。你可能想要标题、链接、标题、表单或表格,然后再决定如何抓取它。

使用 output_format="structured"

from fetch_use import fetch_sync

response = fetch_sync(
    "https://example.com",
    output_format="structured",
)

data = response.json()

print(data)

当你想要在编写更具体的爬虫之前了解页面的结构时,这很有用。

使用会话保持 Cookie

https://browser-use.com/posts/fetch-use#keep-cookies-with-a-session

有些网站只有通过多个请求才有意义。第一个页面设置 Cookie,第二个页面期望它们,第三个页面则根据之前发生的情况而改变。

使用 session_id 来保持状态:

from fetch_use import fetch_sync

session_id = "products-run-1"

home = fetch_sync(
    "https://example.com",
    session_id=session_id,
)

products = fetch_sync(
    "https://example.com/products",
    session_id=session_id,
    output_format="markdown",
)

print(products.text)

使用相同 session_id 的请求共享同一个会话,因此 Cookie 可以从一个请求延续到下一个请求。

发送 POST 请求

https://browser-use.com/posts/fetch-use#send-a-post-request

你也可以将 fetch-use 用于期望数据的端点:

from fetch_use import fetch_sync

response = fetch_sync(
    "https://httpbin.org/post",
    method="POST",
    json_body={
        "query": "wireless headphones",
        "page": 1,
    },
)

print(response.status_code)
print(response.json())

当网站从页面背后的 API 加载数据时,这尤其有用。

处理错误

https://browser-use.com/posts/fetch-use#handle-errors

对于真实的脚本,在解析结果之前,始终检查请求是否成功:

from fetch_use import FetchError, fetch_sync

try:
    response = fetch_sync("https://example.com", output_format="markdown")
    response.raise_for_status()
except FetchError as error:
    print("Request failed:", error)
else:
    print(response.text)

或者直接检查状态码:

if response.status_code == 200:
    print(response.text)
else:
    print("Failed:", response.status_code)

完整示例

https://browser-use.com/posts/fetch-use#complete-example

这是一个小型脚本,它获取一个页面并将可读版本保存到文件中:

from fetch_use import fetch_sync

url = "https://example.com"

response = fetch_sync(url, output_format="markdown")
response.raise_for_status()

with open("page.md", "w", encoding="utf-8") as file:
    file.write(response.text)

print("Saved page.md")

这里是相同的思路,但使用了结构化输出:

import json

from fetch_use import fetch_sync

url = "https://example.com"

response = fetch_sync(url, output_format="structured")
response.raise_for_status()

data = response.json()

with open("page.json", "w", encoding="utf-8") as file:
    json.dump(data, file, indent=2)

print("Saved page.json")

什么时候应该使用浏览器代替?

https://browser-use.com/posts/fetch-use#when-should-you-use-a-browser-instead

fetch-use 最适合当你想要获取页面内容而不启动完整浏览器时。

当页面需要交互时,请使用浏览器自动化:

  • 点击按钮
  • 填写表单
  • 滚动以加载更多数据
  • 完成登录流程
  • 等待 JavaScript 渲染的内容
  • 处理浏览器挑战或验证码

不过,对于许多抓取任务来说,从 fetch-use 开始比首先打开浏览器更快更简单。

如果你确实需要一个真实的浏览器,请使用 Browser Use:

from browser_use import Agent, ChatBrowserUse

agent = Agent(
    task="Go to https://quotes.toscrape.com, and extract the first 5 quotes.",
    llm=ChatBrowserUse(model="bu-2-0"),
)

agent.run_sync()

当你只需要页面内容时,使用 fetch-use。当网站需要点击、滚动、表单、登录或 JavaScript 渲染的内容时,使用 Browser Use。

相似文章