A Python wrapper for the XKCD webcomic API. Because sometimes you need comics in your code.
- Get XKCD comics by number, or just ask for the latest and hope it's not about Python.
- Search comics by title or transcript (for when you remember the joke but not the number).
- Pythonic interface, because we like snakes.
- Type hints, so your editor can feel smart.
- Error handling, because the internet is a scary place.
- Documentation and examples, so you don't have to read the source (unless you want to).
- Python 3.8 or higher (older Pythons are like old comics: fun, but not supported)
- requests (for talking to the internet)
To install the latest stable version:
# Unix / macOS
python3 -m pip install "xkcd.py"
# Windows
py -m pip install "xkcd.py"
To install the development version (for people who like living on the edge):
git clone https://github.com/Ombucha/xkcd.py
cd xkcd.py
python3 -m pip install -e .
- Install the package (see above).
- Start coding! (see below)
import xkcd
# Get the latest comic (fingers crossed it's not about regular expressions)
comic = xkcd.Comic()
print(f"{comic.number}: {comic.title} - {comic.image.url}")
# Get a specific comic by number (353 is a classic)
comic = xkcd.Comic(353)
print(f"{comic.number}: {comic.title} - {comic.image.title}")
# Search for comics about Python (the language, not the snake)
results = xkcd.search("Python")
for comic in results:
print(f"{comic.number}: {comic.title}")
- xkcd (the source of all stick-figure wisdom)
- Official API (for robots)
- Documentation (for humans)
Pull requests, issues, and stick-figure diagrams welcome! See the contributing guide.
MIT License. Because sharing is caring. See the LICENSE file for details.