Recently I found myself needing to download images of all Pokemon for a personal project. And not wanting to download them all by hand I turned to python to help me automate this task.
I firstly needed to find some images of Pokemon on the web, and found the official Pokemon website https://www.pokemon.com/us/
Having been heavily invested in Pokemon in my teen years I knew that the ‘Pokedex’ was the library of Pokemon. On this Pokedex page I found the pokemon listed out nicely in a list with pictures of each one.
Looking closely at the image location for a few images starts to reveal a pattern
https://assets.pokemon.com/assets/cms2/img/pokedex/detail/004.png
https://assets.pokemon.com/assets/cms2/img/pokedex/detail/005.png
https://assets.pokemon.com/assets/cms2/img/pokedex/detail/006.png
The wonderful people at Pokemon.com have put all the images of Pokemon in the same location in ascending (3 digit) order! Now I know the location of all the images of Pokemon, namely “https://assets.pokemon.com/assets/cms2/img/pokedex/detail/001.png” through to “https://assets.pokemon.com/assets/cms2/img/pokedex/detail/807.png“.
A quick Google search reveals how to download files from the web using Python 3, here.
Essentially we want the ‘urlretrieve’ function within the ‘urllib.request’ library. In its simplest form this function accepts as input a url where the file you want to download is, and a location to save the file:
import urllib.request
urllib.request.urlretrieve('url to file location on web', 'location to save file')
All that is left is to figure out how to put it all together:
- iterate through all the locations of Pokemon images
- save each image in a folder with a unique name so they are not overwritten.
What I ended up with was this script to download all of the Pokemon images from pokemon.com:
import urllib.request
baseURL = "https://assets.pokemon.com/assets/cms2/img/pokedex/detail/"
endURL = ".png"
pokemonDirectory = "Folder location to store the images"
for pokemon in range(1, 807):
try:
pokemon = str(pokemon)
if len(pokemon) == 1:
pokemon = "00" + pokemon
elif len(pokemon) == 2:
pokemon = "0" + pokemon
combinedURL = baseURL + pokemon + endURL
savePokemon = pokemonDirectory + pokemon + endURL
urllib.request.urlretrieve(combinedURL, savePokemon)
except:
print("Failed to collect pokemon: " + pokemon)
print("Pokemon Collection Finished")
This worked well enough for me, there are however some options to extend the functionality / usefulness of this:
- Adding a progress bar so you know how much is left to go (can take some time and good to know the process hasn’t stalled).
- Also scraping the names of each Pokemon so they have the names of each, (This wasn’t necessary for my use case)
- Adding log files of some sort (if this is going to be scheduled and or running repeatedly).