Vote count: 0
I've been working for a program over the last few days which should download a range of pages from the webcomic Homestuck. I've created a working version in python 3, but it is horribly inefficient. Can anyone see ways to improve and shorten this code?
import urllib.request
range1 = int(input("Enter the 1st page you want: "))
range2 = int(input("Enter the last page you want: ")) + 1
current = range1 + 1900
final = range2 + 1900
page = ''
nextPage = ''
while current != final:
page = str(current)
nextPage = str(current+1)
while len(page) != 6:
page = '0'+ page
while len(nextPage) != 6:
nextPage = '0'+ nextPage
html = 'http://ift.tt/2lFCCYA'+page
site = urllib.request.urlopen(html)
s = site.read()
s = s.decode("utf8")
s = s.replace("", "")
s = s.replace("http://ift.tt/2l4OndB", "")
s = s.replace("?s=6&p=" + str(nextPage), str(int(nextPage))+".html")
s = s.replace(page+"/"+page, page)
a,b,c = s.split('')
b = "" + b
t = open(str(current)+".html", 'w+')
t.write
t.write(b)
t.close()
page = str((int(page)-1900))
while len(page) != 5:
page = '0'+ page
t = open(str(current)+".html", 'a')
swfname=page+".swf"
t.write("")
t.close()
try:
img = "http://ift.tt/2l4OndB"+page+".gif"
urllib.request.urlretrieve(img, page+".gif")
except:
try:
img = "http://ift.tt/2l4OndB"+page+"_1.gif"
urllib.request.urlretrieve(img, page+"_1.gif")
img = "http://ift.tt/2l4OndB"+page+"_2.gif"
urllib.request.urlretrieve(img, page+"_2.gif")
except:
try:
img = "http://ift.tt/2l4OndB"+page+"/"+page+".swf"
urllib.request.urlretrieve(img, page+".swf")
except:
print("Image "+img+" failed to download")
print ("Page " + str(page) + " of " + str(final-1901) + " downloaded")
current+=1
print("DONE")
asked 5 secs ago
Creating a program to download Homestuck (Python3)
Aucun commentaire:
Enregistrer un commentaire