info@worthwebscraping.com

How to Download Image using Python Web Scraping

How to Download Image using Python Web Scraping

Download Python Script

Send download link to:

https://www.youtube.com/watch?v=6-TtkgunHLA

In this tutorial we will discuss about how to download media from any website, specifically we will look at how to download image using Python from a website and store it in your local system.

We all download a lot of media files from web like images, songs, videos etc. When we have to download only a few files it is ok to do it manually, but if we have to download thousands of files or even hundreds it becomes a tedious task to do manually. Thankfully we can do it using Python in a very easily and fast manner.

In this tutorial, we willbe using Beautiful soap to download images. To download the image we will again be going to IMDB website https://www.imdb.com/list/ls053501318/ and This is an IMDB web site that contains a list of top 50 Hollywood actors .

From this page let us say we are Interested in downloading the images of all the actors, and we want to save those images by the name of those actors.

To grab the name and images, we first need to inspect the webpage to see under which tag they are:

As we can see in above image name is under h3 tag and image is under imgtag and srcattribute. Using this information we can now grab the image and save it by Actor’s name.

See the Detailed code below or watch the video for complete description:

import bs4
import urllib.request
from urllib.request import urlopen
from bs4 import BeautifulSoup as soup

my_url = 'https://www.imdb.com/list/ls053501318/'
#opening up connection, downloading the page
imdb = urlopen(my_url)

#html parsing
bsobj = soup(imdb.read(),'html.parser')

#grabs each Actor
containers =bsobj.findAll('div',{'class':'lister-item mode-detail'})
pic = bsobj.findAll('img')


for container,img in zip(containers,pic):
name = container.h3.a.text.strip()

image = img.get('src')
full_name = str(name) + ".jpg"
urllib.request.urlretrieve(image, full_name)

That’s all on Download Image using Python from web. Want to learn more about how to download videos from any website using Python.