Python to Download XKCD Comics with Full Source Code For Beginners

This script can be used to download any issue of the xkcd comics with just a simple command.

Requirements:

  • beautifulsoup4==4.9.1
  • certifi==2020.6.20
  • chardet==3.0.4
  • idna==2.10
  • requests==2.24.0
  • soupsieve==2.0.1
  • urllib3==1.25.10
  • Install the requirements with the following line:
pip install -r requirements.txt
Code language: CSS (css)

Run the script:

  • Run the following command from your terminal.
python3 xkcd_downloader.py -l 'issue-number'

Example:

python3 xkcd_downloader.py -l 956Code language: JavaScript (javascript)

Source Code:

xkcd_dowloader.py

import requests
from bs4 import BeautifulSoup as bs
import shutil
import argparse

# Code to add the cli

parser = argparse.ArgumentParser()
parser.add_argument("-l", "--issue", required=True, help="Comics Issue Number")
args = vars(parser.parse_args())


#Storing the comic issue number provided by the user
issue_number = args['issue']

#Complete url for the issue
url = "https://xkcd.com/"+ issue_number


response = requests.get(url)

#Checking if we can fetch the url or not
if response.status_code ==200:
    soup = bs(response.content, 'html.parser')
    image_link = soup.find_all('img')[2]['src']
    image_name = image_link.split('/')[-1]
    image_url = "https:" + image_link
    r = requests.get(image_url, stream=True)
    if r.status_code == 200:
        #This ensures the image file is loaded correctly
        r.raw.decode_content = True

        # Creating the image file 
        with open(image_name, 'wb') as f:
            shutil.copyfileobj(r.raw, f)

        print('Image successfully Downloaded: ', image_name)
    else:
        print('Image Couldn\'t be retreived')
else:
    print("Issue number is invalid")Code language: PHP (php)

Leave a Comment