import requests
import anthropic
API_URL = "https://api.webunlocker.gologin.com/api/parsing/v1"
WU_KEY = "YOUR_API_KEY"
def scrape(url: str) -> str:
"""Fetch a URL and return its rendered HTML."""
data = requests.post(
f"{API_URL}/tasks",
headers={"X-API-Key": WU_KEY},
json={"url": url}
).json()
return data["result"]["html"]
# Define the tool for the agent
tools = [
{
"name": "scrape",
"description": "Fetch the rendered HTML of any URL, including JavaScript-heavy and protected sites.",
"input_schema": {
"type": "object",
"properties": {
"url": {"type": "string", "description": "The URL to scrape"}
},
"required": ["url"]
}
}
]
client = anthropic.Anthropic()
messages = [
{"role": "user", "content": "What is the current price of the iPhone 16 Pro on apple.com?"}
]
# Agentic loop
while True:
response = client.messages.create(
model="claude-opus-4-5",
max_tokens=1024,
tools=tools,
messages=messages
)
if response.stop_reason == "end_turn":
print(response.content[0].text)
break
# Handle tool calls
tool_results = []
for block in response.content:
if block.type == "tool_use" and block.name == "scrape":
html = scrape(block.input["url"])
tool_results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": html[:10000] # trim for context window
})
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "user", "content": tool_results})