如何用 Browsa 將網站任務轉化為可逐行呼叫的 API
Turn a website task into an API you can call on rows of input
Browsa 示範先讓 AI 智能體執行並錄製一次網站任務,再對多行輸入重播,輸出欄位一致的結果。示例每行收費 8 credits,三行執行需 39 s,結果可匯出為 CSV 或 JSON。robots API 最多支援 500 rows。
Posted on Nov 24 Edited on Nov 24
Disclosure: I work on Browsa, the cloud browser used here.
An AI agent is good at working out how to do something on a website. It's a waste to have it work it out again for every row of a spreadsheet. The pattern this post shows: let the agent do the task once, record what it did, then replay the recording on each input and get the same typed columns every time.
The example uses books.toscrape.com, a sandbox site made for scraping practice.
1. Do the task once, and record it
import time
from browsa import Client
client = Client() # reads BROWSA_API_KEY
BOOK = "https://books.toscrape.com/catalogue"
job = client.run_task(
task="Open the product page and return title, price and in_stock",
llm="browsa",
initial_url=f"{BOOK}/sharp-objects_997/index.html",
output_schema={
"type": "object",
"properties": {
"title": {"type": "string"},
"price": {"type": "string"},
"in_stock": {"type": "boolean"},
},
"required": ["title", "price", "in_stock"],
},
record_recipe=True,
)
print(f"agent run: {job.outcome} in {job.duration_ms / 1000:.1f} s, {job.credits_charged} credits")
print(job.final_result)
agent run: completed in 11.1 s, 7 credits
{"price": "£47.82", "title": "Sharp Objects", "in_stock": true}
Two details matter here:
-
output_schema: the fields you name become the columns of every later row. Without it you get one free-textanswercolumn. -
initial_url: the page you start on becomes theurlinput of the recording.
2. Save it as a robot with the rows you want
robot = client.robots.create(
name="Book prices",
from_job_id=job.id,
inputs=[
{"url": f"{BOOK}/sharp-objects_997/index.html"},
{"url": f"{BOOK}/soumission_998/index.html"},
{"url": f"{BOOK}/tipping-the-velvet_999/index.html"},
],
)
3. Run it
t0 = time.time()
run = client.robot_runs.wait(client.robots.run(robot.id).id)
print(f"robot run: {run.status} in {time.time() - t0:.0f} s")
print(client.robot_runs.export(run.id)) # CSV
robot run: completed in 39 s
row,status,error_code,credits,input.url,price,title,in_stock
0,ok,,8,https://books.toscrape.com/catalogue/sharp-objects_997/index.html,£47.82,Sharp Objects,true
1,ok,,8,https://books.toscrape.com/catalogue/soumission_998/index.html,£50.10,Soumission,true
2,ok,,8,https://books.toscrape.com/catalogue/tipping-the-velvet_999/index.html,£53.74,Tipping the Velvet,true
Each row replays the recording on its own URL. The columns are the ones from the schema, and the CSV (or JSON) is ready for a sheet or a database.
4. Mistakes are refused, not charged
A row with an input the recording doesn't take is rejected before anything runs:
client.robots.run(robot.id, inputs=[{"link": f"{BOOK}/soumission_998/index.html"}])
UNKNOWN_INPUT inputs[0]: "link" is not a param of this recipe, so the run would ignore it; its params are "url"
Rows that fail while running are free, too.
What it costs, honestly
A row costs the same credits as the agent task it was recorded from: here, 8 per row. Recording doesn't make a row cheaper, and on a page this simple it isn't faster either (39 s for three rows). What it buys you is the same JSON shape on every row, a typed table you can export, and runs that repeat the recorded steps instead of planning them again.
Next
- Run it on a schedule and get an alert only when a value changes: that's the next post in this series.
- The whole robots API (up to 500 rows, CSV/JSON export, health over the last 20 rows): browsa.io/guides/robots.
來源:DEV Community · dev.to