import requests
import anthropic
def scrape(url: str) -> str:
"""Fetch a URL and return its rendered HTML."""
return requests.get(
"https://parsing.webunlocker.gologin.com/v1/scrape",
params={"url": url},
headers={"apikey": "YOUR_API_KEY"}
).text
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?"}
]
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
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]
})
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "user", "content": tool_results})