🚀 We are hiring! See open positions

How to scrape images from a website?

by scrapecrow May 02, 2023

To scrape images from a website we can use Python with HTML parsing tools like beautifulsoup to select all <img> elements and save them.

Here's an example using httpx and beautifulsoup (install using pip install httpx beautifulsoup4):

import asyncio
import httpx
from bs4 import BeautifulSoup
from pathlib import Path


async def download_image(url, filepath, client):
    response = await client.get(url)
    filepath.write_bytes(response.content)
    print(f"Downloaded {url} to {filepath}")


async def scrape_images(url):
    download_dir = Path('images')
    download_dir.mkdir(parents=True, exist_ok=True)

    async with httpx.AsyncClient() as client:
        response = await client.get(url)
        soup = BeautifulSoup(response.text, "html.parser")
        download_tasks = []
        for img_tag in soup.find_all("img"):
            img_url = img_tag.get("src")  # get image url
            if img_url:
                img_url = response.url.join(img_url)  # turn url absolute
                img_filename = download_dir / Path(str(img_url)).name
                download_tasks.append(
                    download_image(img_url, img_filename, client)
                )
        await asyncio.gather(*download_tasks)

# example - scrape all scrapfly blog images:
url = "https://scrapfly.io/blog/"
asyncio.run(scrape_images(url))

Above we are using httpx.AsyncClient to first retrieve the target page HTML. Then, we extract all src attributes of all <img> elements. Finally, we download all images concurrently and save them to ./images directory.

Related Articles

Build a Documentation Chatbot That Works on ANY Website

Build an AI chatbot from any docs site using Scrapfly Crawler API, LangChain, and Streamlit. Works on Cloudflare-protected sites.

PYTHON
AI
RAG
CRAWLING
Build a Documentation Chatbot That Works on ANY Website

LangChain Web Scraping: Build AI Agents & RAG Applications

Learn to integrate LangChain with Scrapfly for web scraping. Build AI agents and RAG applications that extract, process, and understand web data at scale.

AI
PYTHON
LANGCHAIN
SCRAPING
LangChain Web Scraping: Build AI Agents & RAG Applications

Best Web Scraping Tools in 2026

Comprehensive guide to choosing web scraping tools for production. Learn the scraping pipeline framework and how to combine tools like Scrapfly, BeautifulSoup, Playwright, and Scrapy for reliable data extraction at scale.

PYTHON
NODEJS
WEB-SCRAPING
Best Web Scraping Tools in 2026

How to Scrape Facebook: Marketplace and Events

Complete guide to scraping Facebook data including Marketplace listings and Events. Covers authentication, anti-bot bypass, and production-ready techniques.

PYTHON
PLAYWRIGHT
SCRAPEGUIDE
How to Scrape Facebook: Marketplace and Events

Crawl4AI Explained: The AI-Friendly Web Crawling Framework

Discover Crawl4AI, the AI-friendly web crawling framework. Learn features, installation, and intelligent web scraping for LLMs.

WEB-SCRAPING
AI
PYTHON
CRAWLING
Crawl4AI Explained: The AI-Friendly Web Crawling Framework

Social Media Scraping in 2026

Complete guide to scraping Instagram, Twitter, TikTok, and LinkedIn with Python. Learn anti-blocking techniques for 2026.

SCRAPEGUIDE
PYTHON
SOCIAL-MEDIA
Social Media Scraping in 2026