browser-use
thoughts
browser-use is really nice to use. it works nicely and gets the job done.
below are some compiled use cases for to help speed things up.
uv pip install browser-use
uv run patchright install
how to output specific json of data you're scraping:
here is an example for retrieving names of companies in YC X25 batch
from browser_use import Agent, Browser, BrowserConfig, Controller
from langchain_openai import ChatOpenAI
import asyncio
from typing import List
from pydantic import BaseModel
import json
from langchain_google_genai import ChatGoogleGenerativeAI
from dotenv import load_dotenv
load_dotenv()
class Company(BaseModel):
name: str
class Companies(BaseModel):
companies: List[Company]
source_urls: List[str]
BATCH = "X25"
ACCELERATOR = "Y Combinator"
import os
batch_nospace = BATCH.replace(" ", "_")
accelerator_nospace = ACCELERATOR.replace(" ", "_")
output_file = f"{accelerator_nospace}_{batch_nospace}_companies.json"
TASK = (
f"Find the official page(s) for the {ACCELERATOR} {BATCH} batch. "
f"Extract all the names of companies from those pages and return them as a JSON object with two fields: "
f"'companies' (a list of objects with 'name'), and 'source_urls' (a list of the URLs you extracted from)."
)
async def main():
initial_actions = [
{'open_tab': {'url': 'https://google.com'}},
]
browser = Browser(
config=BrowserConfig(
browser_binary_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
)
)
controller = Controller(output_model=Companies)
agent = Agent(
task=TASK,
llm=ChatGoogleGenerativeAI(model='gemini-2.0-flash'),
browser=browser,
controller=controller,
initial_actions=initial_actions
)
history = await agent.run()
result = history.final_result()
if result:
parsed: Companies = Companies.model_validate_json(result)
print("\nExtracted companies:")
for company in parsed.companies:
print(f"- {company.name}")
print("\nSource URLs:")
for url in parsed.source_urls:
print(f"- {url}")
output_dir = os.path.dirname(output_file)
if output_dir:
os.makedirs(output_dir, exist_ok=True)
with open(output_file, "w") as f:
json.dump({
"companies": [c.model_dump() for c in parsed.companies],
"source_urls": parsed.source_urls
}, f, indent=2)
print(f"\nSaved {len(parsed.companies)} companies to {output_file}")
else:
print('No result')
input('Press Enter to close the browser...')
await browser.close()
if __name__ == '__main__':
asyncio.run(main())