所以我正在尝试制作一个下载webcomics的Python脚本,并将它们放在桌面上的文件夹中.我在这里发现了一些类似的程序,但是没有什么比我需要的更好.我发现最相似的那个就在这里(http://bytes.com/topic/python/answers/850927-problem-using-urllib-download-images).我尝试使用此代码:
>>> import urllib
>>> image = urllib.URLopener()
>>> image.retrieve("http://www.gunnerkrigg.com//comics/00000001.jpg","00000001.jpg")
('00000001.jpg', <httplib.HTTPMessage instance at 0x1457a80>)
Run Code Online (Sandbox Code Playgroud)
然后我在计算机上搜索了一个文件"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 …Run Code Online (Sandbox Code Playgroud)