Python interface for the XKCD API

https://github.com/JacobLandau/pykcd

Usage

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]: 'http://imgs.xkcd.com/comics/hdtv.png'

    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 "www.xkcd.com/#/info.0.json". 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?

Why not.

::...

免责声明:
当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 从互联网中抓取并分享;
内容版权归原作者所有;
本人对内容的有效性/合法性不承担任何强制性责任.
若有不妥, 欢迎评注提醒:

蟒营®编程思维提高班 Python版/第11期 正在报名

精品小班/ 每期<42人

扫描报名: 101camp11py

蟒营®式 原创课程

伴你重享学习乐趣

官网: py.101.camp

Reactivate Joy by Self-teching with You


任何问题可先进入知识星球(免费)咨询:
FAQ

关注公众号, 持续获得相关各种咨询:
mainium


追问

任何问题, 随时邮件提问可也:
askdama@googlegroups.com


...::