-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathxkcd.py
executable file
·31 lines (26 loc) · 986 Bytes
/
xkcd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import re
import requests
from bs4 import BeautifulSoup
import random
import os
def download_img(data,filename): #Function to download images
if (os.path.isdir('XKCD')): #Asserts for existence
pass
else:
os.mkdir('XKCD') #If false create a folder
op_file=open('XKCD/'+filename,'wb')
op_file.write(data) #Download off
print "Downloaded",filename
choice=input("How many random images you want to download?? xD \n")
for i in range(choice):
num = str(random.randint(1,1933))
url = "http://xkcd.com/"+num+"/"
r=requests.get(url)
soup=BeautifulSoup(r.content,'html.parser')
filename=str(soup.select('#ctitle')).split('">')
filename=filename[1].split('<')
filename=filename[0] #Getting filename using string manip
img_url=soup.select('#comic')
img_url=str(img_url).split('src=')[1]
img_url='https:'+img_url.split('"')[1]
download_img(requests.get(img_url).content,filename+'.png') #Caling the func i times