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 PyPI

Examples

Basic usage:

from xkcdscrape import xkcd

# Load the archive of comics into a variable
# Allows to get the publication date by passing into getComicInfo and getRandomComic
archive = xkcd.parseArchive()

# Get info about latest comic
info = xkcd.getComicInfo(archive=archive) # w/ date
info = xkcd.getComicInfo() # w/o date

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

# 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, True) # w/ date
info = xkcd.getRandomComic() # w/o date

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

# Get info using the archive dump.
info = xkcd.getComicInfo("dump.json") # latest
info = xkcd.getComicInfo("dump.json", 2000) # specific
info = xkcd.getRandomComic("dump.json", True) # random

# Get latest entry from the RSS feed
# Currently VERY raw, just returns the first <item> tag as a string
lastentry = xkcd.getLastRSS()

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

# xkcd.getComicInfo(2000, archive)
{
    'date': '2018-5-30', 
    'num': '2000', 
    'link': 'https://xkcd.com/2000/', 
    'name': 'xkcd Phone 2000', 
    '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

  • Improve RSS feed output
  • 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.5.0.tar.gz (5.1 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.5.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for xkcd_scrape-0.5.0.tar.gz
Algorithm Hash digest
SHA256 958f45c6957fbd99e2c8cf8369ca6295f5088bb47a6971fd3d5e461f5911ff41
MD5 9b227518e3ee37900537193a9e46c74e
BLAKE2b-256 86563559716d17e89bafce7fc4722b36800e82acea367d6835fc0f8264ef8754

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for xkcd_scrape-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d20f9394308f70b0d21edafbf8298fcc39da760c8199615a2f119524f1f27580
MD5 253ffef51ca805ee61bf9c2a549dbc19
BLAKE2b-256 e767a108522132deb68c95fe9fe71bb8d5b4a0d1bc672284a355794987fe9332

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