Python interface for the XKCD API


The strip object can be initialized like so:

Strip = pykcd.XKCDStrip(strip_number)

The full berth of accessors can be found using the help function. Here's a sampling.

    Alt text

    In [1]: XKCDStrip(50).get_alt_text()
    Out[1]: 'Of course, Penny Arcade has already mocked themselves for this. They don't care."

    Image link

    In [2]: XKCDStrip(732).get_image_link()
    Out[2]: ''

    Downloading Strips

    In [3]: XKCDStrip(178).download_strip()
    100% [...................................] 18611 / 18611
    // Downloaded to /XKCD_Archive/ in the working directory

Under the Hood

Each XKCD strip, barring Strip #404 (Funny funny), has a JSON document located at "". This contains references to data such as the day, month and year published, the strip transcript, the image hotlink, the alt text, and other details. By using the requests library, this document can be grabbed and parsed into a standard Python dictionary, through which the data can be referenced and accessed by it's respective keys.

Image links present a unique challenge in the case of large strips such as Strip #802: Online Communities 2, which have their img key point to a thumbnail rather than the full-resolution hotlink. The solution lies within the link key, which points to a page containing only the full resolution image. We can scrape out the link from this page using BeautifulSoup, and return this value when the user asks for the image link from one of these large strips.

Wget is used in order to download the strips to the '/XKCD_Archive/' folder in the working directory, which will be created if the directory does not already exist. It will check to see if the file is already present, and names the file according to a "Number - Title" scheme. Any characters in the title not friendly with Windows filesystems will be filtered out using a lambda function.

Why not.


当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
若有不妥, 欢迎评注提醒:



关于 ~ DebugUself with DAMA ;-)

关注公众号, 持续获得相关各种嗯哼:

公安备案号: 44049002000656 ...::