Skip to main content

Scrape the XKCD comic archive

Project description

XKCD Scrape

xkcd-scrape is a Python module to dump the XKCD.com archive and get comic info using BS4. Honestly, it's a very basic module with one premise - easily get information about comics.

status-badge

Examples

Basic usage:

from xkcdscrape import xkcd

# Load the archive of comics into a variable
archive = xkcd.parseArchive()

# Get info about latest comic
info = xkcd.getComicInfo(archive)

# Get info about specific comic
# The comic can either be an int (200), a str ("200"|"/200/"), or a link ("https://xkcd.com/200")
info = xkcd.getComicInfo(archive, 2000)

# Get info about a random comic
# Passing the second paramenter as True makes the module only fetch comics that are present in the archive
info = xkcd.getRandomComic(archive, False)

# You can pass None for archive, in that case there won't be a date in response
# When passing None to getRandomComic - fromArchive will always be True
info = xkcd.getComicInfo(None, 2000)
info = xkcd.getRandomComic(None)

# Dump archive to file
xkcd.dumpToFile(archive, "dump.json")

# Get info using archive dump. You can do the same with getRandomComic()
info = xkcd.getComicInfo("dump.json", 2000)

The getComicInfo function (also called inside of getRandomComic) returns a dict with following keys:

# xkcd.getComicInfo(archive, 2000)
{
    'num': '2000', 
    'link': 'https://xkcd.com/2000/', 
    'name': 'xkcd Phone 2000', 
    'date': '2018-5-30', 
    'image': 'https://imgs.xkcd.com/comics/xkcd_phone_2000.png', 
    'title': 'Our retina display features hundreds of pixels per inch in the central fovea region.'
}

As you can see, it returns the following list of keys:

  • num - comic number
  • link - hyperlink to comic
  • name - the name of the comic
  • date - YYYY-MM-DD formatted date of when the comic was posted (not returned if archive is None)
  • image - hyperlink to image used in the comic
  • title - title (hover) text of the comic

Archive

The XKCD archive is where we get the list of comics, as well as their names and date of posting. This is the only place where we can get the date of posting, so it's required if you need the date.

The archive is a dict containing various dicts with keys of /num/. Example:

{
    ...,
    "/2000/": {
        "date": "2018-5-30", 
        "name": "xkcd Phone 2000"
    },
    ...
}

Tests

Tests can be run from the project's shell after installing and activating the venv using poetry run pytest. Use Python's included unittest module for creating tests (examples are in tests/).

TODO

  • Add shields.io badges when 525 errors self-resolve
  • Add RSS/Atom feed support to fetch latest comic
  • API and homepage (on one domain)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xkcd_scrape-0.4.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xkcd_scrape-0.4.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file xkcd_scrape-0.4.0.tar.gz.

File metadata

  • Download URL: xkcd_scrape-0.4.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.0 Linux/5.10.0-19-amd64

File hashes

Hashes for xkcd_scrape-0.4.0.tar.gz
Algorithm Hash digest
SHA256 8b824a0130a0c21c1a443bb8b3f8e7ca02b3b3215abc0ead776ccdb7102a45a9
MD5 792f68fae915fcd02d2c88dcd52cf84d
BLAKE2b-256 5348d0032d0df9a6e541a8d6a7a89b7f5cfa08d4257a24f3a62c1413c88df5a5

See more details on using hashes here.

File details

Details for the file xkcd_scrape-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: xkcd_scrape-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.0 Linux/5.10.0-19-amd64

File hashes

Hashes for xkcd_scrape-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ecb5305236f4095507641e6c89df7817fddc42ca2c37a6679e00a9036e4bf1c3
MD5 1b897217f68ac45d154784531d090450
BLAKE2b-256 30c20cadb7afe32af54eca27d26c9d4df35ebffd9fad1e540101fc250a54c4d3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page