Open In App

Image Scraping with Python

Scraping Is a very essential skill for everyone to get data from any website. In this article, we are going to see how to scrape images from websites using python. For scraping images, we will try different approaches.

Method 1: Using BeautifulSoup and Requests



pip install bs4
pip install requests

Approach:

Implementation:






import requests 
from bs4 import BeautifulSoup 
    
def getdata(url): 
    r = requests.get(url) 
    return r.text 
    
htmldata = getdata("https://www.geeksforgeeks.org/"
soup = BeautifulSoup(htmldata, 'html.parser'
for item in soup.find_all('img'):
    print(item['src'])

Output:

https://media.geeksforgeeks.org/wp-content/cdn-uploads/20201018234700/GFG-RT-DSA-Creative.png
https://media.geeksforgeeks.org/wp-content/cdn-uploads/logo-new-2.svg

Method 2: Using urllib and BeautifulSoup

urllib : It is a Python module that allows you to access, and interact with, websites with their URL. To install this type the below command in the terminal.

pip install urllib

Approach:

Implementation:




from urllib.request import urlopen
from bs4 import BeautifulSoup
  
htmldata = urlopen('https://www.geeksforgeeks.org/')
soup = BeautifulSoup(htmldata, 'html.parser')
images = soup.find_all('img')
  
for item in images:
    print(item['src'])

Output:

https://media.geeksforgeeks.org/wp-content/cdn-uploads/20201018234700/GFG-RT-DSA-Creative.png
https://media.geeksforgeeks.org/wp-content/cdn-uploads/logo-new-2.svg


Article Tags :