Skip to content

Ombucha/xkcd.py

https://raw.githubusercontent.com/Ombucha/xkcd.py/main/banner.png

PyPI version PyPI downloads Lines of code Repository size

A Python wrapper for the XKCD webcomic API. Because sometimes you need comics in your code.

Features

  • 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).

Requirements

  • Python 3.8 or higher (older Pythons are like old comics: fun, but not supported)
  • requests (for talking to the internet)

Installation

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 .

Getting Started

  1. Install the package (see above).
  2. Start coding! (see below)

Quick Example

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}")

Links

Contributing

Pull requests, issues, and stick-figure diagrams welcome! See the contributing guide.

License

MIT License. Because sharing is caring. See the LICENSE file for details.