0

I found this python script online that downloads comics. When I tried to run it I got the error: "Traceback (most recent call last): File "/Users/Jake/Desktop/garfield-downloader-master/garfield.py", line 10, in import urllib2 ModuleNotFoundError: No module named 'urllib2'" Code is below.

from datetime import date,timedelta
import urllib2
import os

def daterange(start,end):
    for x in range(int ((end - start).days)):
        yield start + timedelta(x)

#Create the Directory, if not already existing
dir = "./garfield/"
if not os.path.exists(dir):
    os.makedirs(dir)
    
start_date = date.today()
    
#Garfield Started on 19th June, 1978
start_date = start_date.replace(year=1978,month=6,day=19)
end_date = date.today()

for single_day in daterange(start_date,end_date):
    link = "https://d1ejxu6vysztl5.cloudfront.net/comics/garfield/" + (str(single_day)[:4]) + "/" + str(single_day) + ".gif"
    print(single_day)

    #checks whether the files already exist or not  
    filename = dir+str(single_day)+".gif"
    if not os.path.exists(filename): 
        try:
            f = urllib2.urlopen(link)
        except urllib2.URLError:
            if e.code == 404:
                continue
        print ("Downloading :: " + link)
    
        save_file = open(dir+str(single_day)+".gif","w"+'b')
        save_file.write(f.read())
        save_file.close()

print("Garfield Comics has been downloaded")

2 Answers2

0
from datetime import date,timedelta
import urllib2
import os

def daterange(start,end):
    for x in range(int ((end - start).days)):
        yield start + timedelta(x)

#Create the Directory, if not already existing
dir = "./garfield/"
if not os.path.exists(dir):
    os.makedirs(dir)
    
start_date = date.today()
    
#Garfield Started on 19th June, 1978
start_date = start_date.replace(year=1978,month=6,day=19)
end_date = date.today()

for single_day in daterange(start_date,end_date):
    link = "https://d1ejxu6vysztl5.cloudfront.net/comics/garfield/" + (str(single_day)[:4]) + "/" + str(single_day) + ".gif"
    print(single_day)

    #checks whether the files already exist or not  
    filename = dir+str(single_day)+".gif"
    if not os.path.exists(filename): 
        try:
            f = urllib2.urlopen(link)
        except urllib2.URLError as e :
            if e == 404:
                continue
        print ("Downloading :: " + link)
    
        save_file = open(dir+str(single_day)+".gif","w"+'b')
        save_file.write(f.read())
        save_file.close()

print("Garfield Comics has been downloaded")

This script has a lot of issues but this code here downloads something at least.

urllib2 is a module included with Python 2. So run with Python2 and you'll have no issues aside from some bugs/errors.

Edit: I would be careful when installing urllib packages. From reading/research, some pkgs are deployed as possible malware. See here for more info

kc101010
  • 41
  • 1
  • 5
0

As the docs state, urllib2 has been split across several modules in Python 3, namely urllib.request and urllib.error implementing the same methods you are calling. E.g.,

from urllib.request import urlopen
Max Shouman
  • 1,333
  • 1
  • 4
  • 11