1

scrape free stock images from websites using python

scrape free stock images from websites using python

Topics : scrape free stock images from websites using python , scrape images from the website , scrape images using python

problem statements:

  • scrape images from the website
  • scrape images using python
  • How to scrape images from websites
  • images scraping using beautiful soup and requests
  • python images web scraping
  • downloading images using python
  • image scraping
  • image scraping python

scrape free stock images from websites using python – Photos are being used more frequently on websites, blogs, slideshows, and ebooks. The need for photos has increased dramatically and more people are using stock photo agencies to buy their photos and images, protect their companies from copyright issues and utilize the many options that stock photo agencies offer.

If we need free stock images in bulk then downloading it from website is much time consuming and not a easy task,

So can we use python to automate the downloading process of free stock images by writing a few lines of code?

The answer is Yes, Python urllib and Beautiful Soup module allow us to scrape/download images from websites. So I will tell you how to use python to automate the boring stuff of downloading images manually, So without wasting the time let get started our article scrape free stock images from websites using python,

Beautiful Soup is a Python library for pulling data out of HTML and XML files. It works with your favorite parser to provide idiomatic ways of navigating, searching, and modifying the parse tree. It commonly saves programmers hours or days of work.

Make sure you have downloaded and installed python if you haven’t download python yet then follow the tutorial to download python :

Downloading and setting up python

Once you have down with downloading and setting up python Then Next Thing you need to download the Python urllib and Beautiful Soupmodule.

Module You need to scrape free stock images from websites using python?

Just open command prompt and run the following commands

pip install urllib3
pip install bs4

After installing urllib3 Next is to create a python project, If you still don’t know how to create a python project then follow the tutorial link below :

Creating project in python

After this paste the code below in that python file

CODE:

import urllib.request
import bs4 as bs

images_scraped=1 
mylist=[] 
term = input("Enter Search term : ")
url = 'https://www.istockphoto.com/search/2/image?phrase='+term 
html = urllib.request.urlopen(url)
soup = bs.BeautifulSoup(html,'lxml') 
for res in soup.findAll('img'):
     try:
         
         num = res.get('src')
         if num in mylist:               
             continue
         else:
             URL = res.get('src')      
             urllib.request.urlretrieve(URL, term+str(images_scraped)+".jpeg")    
             num = num.split('-')
             num = num[-1]
             
             mylist.append(num)
             print(" item ",images_scraped)
             images_scraped+=1
     except Exception as e:     
         pass
         print(e)

After running this code. It will input the search time and auto-downloads all available images for that term in the current working directory.

if you can answer the following question that will be helpful for others.

Q1. How can we limit the number of images scraped/downloaded?

Q2. How can we change the directory for images scraped/downloaded?

If you have another efficient way to perform this task of scraping free stock images from websites using python. Please share in the comment section.

DEMO:

Hope you like the article scrape free stock images from websites using python

keep visiting : shaadcoding

shaad

One Comment

  1. This is very interesting post. Thank you for sharing this informative guidelines.

Leave a Reply

Your email address will not be published. Required fields are marked *