In due parti. Sto cercando di scaricare più podcast archiviati Cory Doctorow dall'archivio Internet. Quelli vecchi che non entrano nel mio feed di iTunes. Ho scritto lo script ma i file scaricati non sono formattati correttamente.Come posso scaricare un file zip in python usando urllib2?
Q1 - Cosa cambio per scaricare i file zip mp3? Q2 - Qual è un modo migliore per passare le variabili in 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
Questo script è stato adattato da here
funzionato come un campione. Grazie! – Justjoe
Solo un'ultima domanda dove posso saperne di più sul "% d" utilizzato nella URL, io sono ancora un po 'abbozzato su come sta funzionando. – Justjoe
Controlla la documentazione per la formattazione di stringhe: http://docs.python.org/library/stdtypes.html#string-formatting – dcolish