Last active
May 20, 2020 09:22
-
-
Save ActuallyDaneel/c127fe6ec5a3a290f0fac62e90b1d3b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import shutil | |
from bs4 import BeautifulSoup | |
errorcounter = 0 | |
counter = 0 | |
for i in range(int(input("input a number greater than 1: "))): | |
try: | |
if errorcounter < 11: | |
counter += 1 | |
xkcd_url = "https://xkcd.com/" + str(counter) | |
markup = requests.get(xkcd_url).text | |
soup = BeautifulSoup(markup, "html.parser") | |
middleContainer_content = soup.findAll("img") | |
img_urls = [] | |
for i in middleContainer_content: | |
if i["src"] != None: | |
img_urls.append(i["src"]) | |
image_url = "http:"+ img_urls[1] | |
filename = str(counter) + image_url.split("/")[-1] | |
r = requests.get(image_url, stream = True) | |
if r.status_code == 200: | |
r.raw.decode_content = True | |
with open(filename,'wb') as f: | |
shutil.copyfileobj(r.raw, f) | |
print('Image sucessfully Downloaded: ',filename) | |
else: | |
print('Image Couldn\'t be retreived') | |
else: | |
continue | |
except Exception as e: | |
errorcounter += 1 | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment