所以我正在尝试编写一个Python脚本,用于下载网络漫画并将其放在我的桌面文件夹中。在这里我发现了一些类似的程序可以实现类似的功能,但没有完全符合我的需求。我找到的最相似的一个是这个(http://bytes.com/topic/python/answers/850927-problem-using-urllib-download-images)。我尝试使用此代码:
我在电脑上搜索了一个文件名为“00000001.jpg”的文件,但我只找到了它的缓存图片。我甚至不确定它是否保存在我的电脑上。一旦我理解如何下载该文件,我认为我知道如何处理剩下的部分了。基本上只需使用for循环并将字符串拆分为“00000000”和“jpg”,并将“00000000”递增到最大数字,我需要某种方式来确定这个数字。您有关于如何完成此操作或正确下载文件的任何建议吗?
谢谢!
编辑6/15/10
以下是已完成的脚本,它可以将文件保存到您选择的任何目录中。由于某些奇怪的原因,文件未能下载,现在它们已经下载成功。如果您有任何清理建议,将不胜感激。我正在努力找出网站上有多少漫画,以便我只能获取最新的一本,而不是在发生一定数量的异常后程序退出。
>>> import urllib
>>> image = urllib.URLopener()
>>> image.retrieve("http://www.gunnerkrigg.com//comics/00000001.jpg","00000001.jpg")
('00000001.jpg', <httplib.HTTPMessage instance at 0x1457a80>)
我在电脑上搜索了一个文件名为“00000001.jpg”的文件,但我只找到了它的缓存图片。我甚至不确定它是否保存在我的电脑上。一旦我理解如何下载该文件,我认为我知道如何处理剩下的部分了。基本上只需使用for循环并将字符串拆分为“00000000”和“jpg”,并将“00000000”递增到最大数字,我需要某种方式来确定这个数字。您有关于如何完成此操作或正确下载文件的任何建议吗?
谢谢!
编辑6/15/10
以下是已完成的脚本,它可以将文件保存到您选择的任何目录中。由于某些奇怪的原因,文件未能下载,现在它们已经下载成功。如果您有任何清理建议,将不胜感激。我正在努力找出网站上有多少漫画,以便我只能获取最新的一本,而不是在发生一定数量的异常后程序退出。
import urllib
import os
comicCounter=len(os.listdir('/file'))+1 # reads the number of files in the folder to start downloading at the next comic
errorCount=0
def download_comic(url,comicName):
"""
download a comic in the form of
url = http://www.example.com
comicName = '00000000.jpg'
"""
image=urllib.URLopener()
image.retrieve(url,comicName) # download comicName at URL
while comicCounter <= 1000: # not the most elegant solution
os.chdir('/file') # set where files download to
try:
if comicCounter < 10: # needed to break into 10^n segments because comic names are a set of zeros followed by a number
comicNumber=str('0000000'+str(comicCounter)) # string containing the eight digit comic number
comicName=str(comicNumber+".jpg") # string containing the file name
url=str("http://www.gunnerkrigg.com//comics/"+comicName) # creates the URL for the comic
comicCounter+=1 # increments the comic counter to go to the next comic, must be before the download in case the download raises an exception
download_comic(url,comicName) # uses the function defined above to download the comic
print url
if 10 <= comicCounter < 100:
comicNumber=str('000000'+str(comicCounter))
comicName=str(comicNumber+".jpg")
url=str("http://www.gunnerkrigg.com//comics/"+comicName)
comicCounter+=1
download_comic(url,comicName)
print url
if 100 <= comicCounter < 1000:
comicNumber=str('00000'+str(comicCounter))
comicName=str(comicNumber+".jpg")
url=str("http://www.gunnerkrigg.com//comics/"+comicName)
comicCounter+=1
download_comic(url,comicName)
print url
else: # quit the program if any number outside this range shows up
quit
except IOError: # urllib raises an IOError for a 404 error, when the comic doesn't exist
errorCount+=1 # add one to the error count
if errorCount>3: # if more than three errors occur during downloading, quit the program
break
else:
print str("comic"+ ' ' + str(comicCounter) + ' ' + "does not exist") # otherwise say that the certain comic number doesn't exist
print "all comics are up to date" # prints if all comics are downloaded
urllib.request.URLopener()
中。 - Filippo Mazza