Skip to content

make_something

Make Python fetch you a real dog photo

This is the end result, so you know where you are going before you type anything. You run eight lines. Your computer goes off to the internet, finds a photo of a real dog, and saves it into your folder. A different dog every time you run it.

what your screen says

Your dog is here: https://images.dog.ceo/breeds/airedale/n02096051_9391.jpg Saved it as dog.jpg

And there is now a file called dog.jpg sitting next to your program. Open it. That is your dog.

Here is the whole thing

Not a piece of it. All of it. Copy it exactly, including the spaces at the start of the line under with.

import urllib.request, json

url = "https://dog.ceo/api/breeds/image/random"

with urllib.request.urlopen(url) as answer:
    dog = json.load(answer)

picture = dog["message"]
print("Your dog is here:", picture)

urllib.request.urlretrieve(picture, "dog.jpg")
print("Saved it as dog.jpg")

There is nothing to install. Everything here comes with Python already. On a phone the code slides sideways, so use the copy button rather than typing out what you can see.

How to run it

  1. Open a Python editor. If Python is already on your computer, you have one called IDLE. Search your machine for it. If you have nothing, ask an adult before installing anything.
  2. Make a new file and save it as dog.py. Remember which folder you saved it in, because your dog photo lands in the same one.
  3. Paste the code in and save again.
  4. Run it. In IDLE that is the Run menu, then Run Module. Your screen prints the two lines you saw at the top of this page.
  5. Open the folder and look for dog.jpg. Run the program again and you get a different dog.

if_it_breaks

Red text is not you failing. It is the computer telling you exactly which line it did not understand. Look at the line number it gives you, check that line against the one above, and fix the one character that is different. That is what programmers do all day.

What just happened, and what an API is

You just used an API. You had already used one before you knew the word, which is the easiest way to learn it.

An API is a doorway a website leaves open on purpose, so that programs can ask it for something instead of people clicking around. A person visits a website and reads a page. A program visits an API and gets back plain facts it can use.

The website you asked is dog.ceo. It keeps a big pile of dog photos and it leaves one doorway open that means "give me a random one". That doorway is the address in your code. You did not ask a human. Your program asked their computer, and their computer answered in about a second.

What the answer actually looked like

The reply did not arrive as a picture. It arrived as a small piece of text, laid out in a shape called JSON, which is just a tidy way of writing facts so a program can find things in it:

{ "message": "https://images.dog.ceo/breeds/airedale/n02096051_9391.jpg", "status": "success" }

Your line dog["message"] reaches into that and pulls out the bit labelled message, which is the address of the photo. Then the last line goes and downloads it.

That is the whole idea, and it is the same idea behind the big ones. When an app shows you the weather, it is asking a weather API. When a website takes a payment, it is asking a payment API. You have just done the same kind of thing they do.

Now change it

Running someone else's code teaches you very little. Breaking it and fixing it teaches you a lot. Try these in order.

easy

Rename the photo

Change "dog.jpg" to "goodboy.jpg". Run it. Find the new file.

easy

Get ten dogs

Ask an adult or search how to repeat something ten times in Python. Save each one with a different name, or you will keep writing over the same dog.

harder

Pick the breed

The same site has a doorway for one breed at a time. Swap the address for https://dog.ceo/api/breed/husky/images/random and see what changes.

harder

Break it deliberately

Spell message wrongly and run it. Read the error. Now you know what that particular error looks like, which means you will recognise it next time.

next

Get five dogs without writing the change yourself

Once this one runs, the next page has you ask an AI assistant to change it, and then shows you how it can hand back code that says it worked when it did not. Ask AI to change your code, then check it really worked.

for_the_adult_in_the_room

This program contacts one outside website, dog.ceo, which is a free public service widely used for teaching. It downloads one image file into the folder the program is saved in. It sends nothing about your child, asks for no account and needs no payment details. If you would rather your child did not download files, they can delete the last two lines and the program will simply print the address instead.

All the things you can make

your_progress

Got your dog photo? Tick it off.

Kept in this browser only. No account, no sign up, and nothing about you reaches us.