Get Random Dog Images in Python


There are several ways to get random dog images in Python. Here are three methods:

Method 1: Using the requests and json modules

import requests
import json

response = requests.get('https://dog.ceo/api/breeds/image/random')
data = json.loads(response.text)
image_url = data['message']

In this method, we use the requests module to send a GET request to the Dog API (https://dog.ceo/api/breeds/image/random) which returns a JSON response containing a random dog image URL. We then use the json module to parse the JSON response and extract the image URL from the 'message' key.

Method 2: Using the random and webbrowser modules

import random
import webbrowser

breeds = ['affenpinscher', 'african', 'airedale', 'akita', 'appenzeller', 'australian', 'basenji', 'beagle', 'bluetick', 'borzoi', 'bouvier', 'boxer', 'brabancon', 'briard', 'bulldog', 'bullterrier', 'cairn', 'cattledog', 'chihuahua', 'chow', 'clumber', 'cockapoo', 'collie', 'coonhound', 'corgi', 'cotondetulear', 'dachshund', 'dalmatian', 'dane', 'deerhound', 'dhole', 'dingo', 'doberman', 'elkhound', 'entlebucher', 'eskimo', 'frise', 'germanshepherd', 'greyhound', 'groenendael', 'havanese', 'hound', 'husky', 'keeshond', 'kelpie', 'komondor', 'kuvasz', 'labrador', 'leonberg', 'lhasa', 'malamute', 'malinois', 'maltese', 'mastiff', 'mexicanhairless', 'mix', 'mountain', 'newfoundland', 'otterhound', 'papillon', 'pekinese', 'pembroke', 'pinscher', 'pitbull', 'pointer', 'pomeranian', 'poodle', 'pug', 'puggle', 'pyrenees', 'redbone', 'retriever', 'ridgeback', 'rottweiler', 'saluki', 'samoyed', 'schipperke', 'schnauzer', 'setter', 'sheepdog', 'shiba', 'shihtzu', 'spaniel', 'springer', 'stbernard', 'terrier', 'vizsla', 'weimaraner', 'whippet', 'wolfhound']

breed = random.choice(breeds)
response = requests.get(f'https://dog.ceo/api/breed/{breed}/images/random')
data = json.loads(response.text)
image_url = data['message']

webbrowser.open(image_url)

In this method, we use the random module to choose a random dog breed from a list of breeds. We then send a GET request to the Dog API with the chosen breed to get a random image URL for that breed. We use the webbrowser module to open the image URL in the default web browser.

Method 3: Using the dogapi module

import dogapi

api_key = 'YOUR_API_KEY'
api = dogapi.DogAPI(api_key)

response = api.breeds_random_image('dog')
image_url = response['message']

In this method, we use the dogapi module which provides a Python wrapper for the Dog API. We first need to obtain an API key from the Dog API website. We then create a DogAPI object with our API key and use the breeds_random_image method to get a random dog image URL. We extract the image URL from the 'message' key in the response dictionary.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.