Skip to content
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
50 lines (37 sloc) 1.3 KB
Written by: Shreyas Daniel -
Written on: 26 April 2017
Description: Download latest XKCD Comic with this program.
if this script is launched from the cloned repo, a new folder is created.
Please move the file to another directory to avoid messing with the folder structure.
import requests
from lxml import html
import urllib.request
import os
def main():
# opens
page = requests.get("")
except requests.exceptions.RequestException as e:
print (e)
# parses page
tree = html.fromstring(page.content)
# finds image src url
image_src = tree.xpath(".//*[@id='comic']/img/@src")[0]
image_src = "https:" + str(image_src)
# gets comic name from the image src url
comic_name = image_src.split('/')[-1]
# save location of comic
comic_location = os.getcwd() + '/comics/'
# checks if save location exists else creates
if not os.path.exists(comic_location):
# creates final comic location including name of the comic
comic_location = comic_location + comic_name
# downloads the comic
urllib.request.urlretrieve(image_src, comic_location)
if __name__ == "__main__":
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.