Python: Download All Images from HTML Page – A Step Guide

In this tutorial, we will use some steps to introduce you how to download all images in a html page using python.

Python: Download All Images from HTML Page - A Step Guide

1.Install some libraries

pip install httplib2
pip install bs4
pip install urllib

2.Import library

import httplib2
from bs4 import BeautifulSoup, SoupStrainer

3.Determine a html page

url = 'https://www.cocyer.com/'

3.Scrape html page using httplib2

http = httplib2.Http()
response, content = http.request(url)

4.Extract all image links using BeautifulSoup

images =  BeautifulSoup(content).find_all('img')

image_links =[]

for image in images:
    image_links.append(image['src'])

5.Start to download images by links

for link in image_links:
            
    filename = link.split("/")[-1].split("?")[0]
    urllib.request.urlretrieve(image_url, filename=filename)

In this code, we will get image file name by its url, then use urllib.request.urlretrieve() to download an image.