两个问题。我想从互联网档案馆下载多个归档的Cory Doctorow播客。这些旧的播客没有出现在我的iTunes订阅中。我已经编写了脚本,但是下载的文件格式不正确。
问题1- 我该如何更改才能下载zip mp3文件? 问题2- 有什么更好的方法将变量传递到URL中?
# and the base url.
def dlfile(file_name,file_mode,base_url):
from urllib2 import Request, urlopen, URLError, HTTPError
#create the url and the request
url = base_url + file_name + mid_url + file_name + end_url
req = Request(url)
# Open the url
try:
f = urlopen(req)
print "downloading " + url
# Open our local file for writing
local_file = open(file_name, "wb" + file_mode)
#Write to our local file
local_file.write(f.read())
local_file.close()
#handle errors
except HTTPError, e:
print "HTTP Error:",e.code , url
except URLError, e:
print "URL Error:",e.reason , url
# Set the range
var_range = range(150,153)
# Iterate over image ranges
for index in var_range:
base_url = 'http://www.archive.org/download/Cory_Doctorow_Podcast_'
mid_url = '/Cory_Doctorow_Podcast_'
end_url = '_64kb_mp3.zip'
#create file name based on known pattern
file_name = str(index)
dlfile(file_name,"wb",base_url
这个脚本是从这里改编的。
f.read()
会在将文件写入磁盘之前将整个文件存储在内存中,如果文件太大可能会失败。 - Maxime