Skip to content

ImageScrapper

Nuhman Pk edited this page Feb 12, 2023 · 1 revision

The ImageScrapper class allows you to scrape images from a given URL, either a single image or all images present on the URL, or images with a specific pattern in their URL To use the ImageScrapper class, add the following import statement:

from Webtrench import ImageScrapper

1. Image from URL

The image_from_url method takes in a url as a mandatory argument and an optional folder_path argument. This method downloads a single image from the given URL and saves it to the specified folder. If no folder is specified, it saves the image in the current working directory.

Here's an example of how to use the image_from_url method:

ImageScrapper.image_from_url("https://example.com/image.jpg")

If you want to save the image in a specific folder, you can specify the folder path as follows:

ImageScrapper.image_from_url("https://example.com/image.jpg", "images")

In case of any error while downloading the image, an exception will be raised. You can handle the exception as follows:

try:
    ImageScrapper.image_from_url("https://example.com/image.jpg")
except Exception as e:
    print(e)

2. All Images from URL

The all_image_from_url method takes in a url as a mandatory argument and an optional folder_path argument. This method downloads all images from the given URL and saves them to the specified folder. If no folder is specified, it saves the images in the current working directory.

Here's an example of how to use the all_image_from_url method:

ImageScrapper.all_image_from_url("https://example.com")

If you want to save the images in a specific folder, you can specify the folder path as follows:

ImageScrapper.all_image_from_url("https://example.com", "images")

In case of any error while downloading the images, an exception will be raised. You can handle the exception as follows:

try:
    ImageScrapper.all_image_from_url("https://example.com")
except Exception as e:
    print(e)

3. Images with URL Pattern

The image_with_url_pattern method takes in a url as a mandatory argument, a pattern as a mandatory argument, and an optional folder_path argument. This method downloads all images from the given URL that match the specified pattern and saves them to the specified folder. If no folder is specified, it saves the images in the current working directory.

Here's an example of how to use the image_with_url_pattern method:

from Webtrench import ImageScapper

# Example usage of image_with_url_pattern() method

# Define the URL to scrape images from
url = "https://www.example.com"

# Define the pattern of the image URL that you want to scrape
pattern = "example-image"

# Define the folder path to save the scraped images (default is the current working directory)
folder_path = "./scraped_images"

# Call the image_with_url_pattern() method
ImageScapper.image_with_url_pattern(url, pattern, folder_path)
Clone this wiki locally