【发布时间】:2011-05-01 00:46:03
【问题描述】:
两部分问题。我正在尝试从 Internet 存档下载多个存档的 Cory Doctorow 播客。旧的没有进入我的 iTunes 提要。我已经编写了脚本,但是下载的文件格式不正确。
Q1 - 下载 zip mp3 文件需要做些什么更改? Q2 - 将变量传递到 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
此脚本改编自here
【问题讨论】: