For AI agents: a documentation index is available at /llms.txt
Skip to main content

Extract and Download Images

Find all images on a page and download them to disk using a headless browser that can access JavaScript-rendered content.

Prerequisites

Steps

Use the /scrape REST endpoint to extract image URLs without opening a browser connection.

View Full Code on GitHub

1. Build the request

Use the /scrape endpoint to extract all <img> src attributes from the page:

https://production-sfo.browserless.io/scrape?token=YOUR_API_TOKEN_HERE

2. Extract URLs and download each image

The src is nested inside each result's attributes array. Use jq to pull it out, then loop and download:

URLS=$(curl -s -X POST \
"https://production-sfo.browserless.io/scrape?token=YOUR_API_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d '{
"url": "https://example.com",
"elements": [{ "selector": "img", "timeout": 5000 }]
}' | jq -r '.data[0].results[].attributes[] | select(.name=="src") | .value')

mkdir -p images
i=0
while IFS= read -r url; do
curl -sL "$url" --output "images/image-$i.jpg"
echo "Saved images/image-$i.jpg"
i=$((i + 1))
done <<< "$URLS"

Next steps