#!/usr/bin/env pythonimport os, re, mechanizesepara = "/"dir_actual = os.getcwd()carpeta_descargas= "descargas"# Funcionesdef cabecera(): print "\n" print "[A]uto[D]ownload[S]cript [v0.1]" print "by [email protected]" print "*********************************\n"def obtieneDatos(): website = raw_input("Ingrese Sitio Web > ") #website = "http://www.dkv.columbia.edu/video/Vin/Music/" website = website if re.match("^http",website) else "http://"+website destino = raw_input("Carpeta Descargas > ") #destino = "prueba" ruta_destino = creaCarpeta(dir_actual+separa+carpeta_descargas,destino) return website, ruta_destinodef creaCarpeta(ruta,carpeta): try: if not os.path.isdir(ruta+separa+carpeta): os.mkdir(ruta+separa+carpeta) return ruta+separa+carpeta except: return ruta def buscarSubsitios(sitio,ruta_destino): br = mechanize.Browser() br.set_handle_robots(False) br.addheaders = [('User-Agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5')] br.open(sitio) #Baja los archivos correspondientes a la regexp bajarArchivos(br.links(url_regex=re.compile(r"\.(pdf|doc|rar|zip|mp3|docx|jpg|gif|png|xls|xlsx|ppt|pptx|exe)$")),ruta_destino,1) bajarArchivos(br.links(url_regex=re.compile(r"\.(txt|csv|log|js|html|xml|inf|ini|conf|sql)$")),ruta_destino,0) #Busca subdirectorios links = br.links(url_regex=re.compile(r"/$")) for link in links: if link.absolute_url > sitio : v_url = link.url.replace("%20", " ") nva_ruta = creaCarpeta(ruta_destino,v_url) if ruta_destino == nva_ruta: continue print " > Creando ["+nva_ruta+"]" buscarSubsitios(link.absolute_url,nva_ruta) br = None def bajarArchivos(links,directorio,binario): modo = "wb" if binario==1 else "w" for link in links: br_bajar = mechanize.Browser() br_bajar.set_handle_robots(False) br_bajar.addheaders = [('User-Agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5')] v_url = link.url.replace("%20", " ") print " > Descargando ["+v_url+"]" try: datos = br_bajar.open(link.absolute_url).read() except: print " > Error intentando acceder al archivo ["+link.absolute_url+"]" continue finally: br_bajar = None nombre = directorio+separa+v_url try: fichero = open(nombre,modo) fichero.write(datos) fichero.close() except: print " > Error intentando guardar el archivo ["+nombre+"]" continue # Ejecucionruta_descargas = creaCarpeta(dir_actual,carpeta_descargas)cabecera()sitio, ruta_destino = obtieneDatos()buscarSubsitios(sitio,ruta_destino)raw_input("\nPresiona [enter] para salir...")
import os, re, mechanizeImportError: No module named mechanize
Se produce por que no tienes el módulo, tienes que descargarlo y agregarlo a tus librerías de python en python27/lib/sites-packages aunque esto se suele instalar solo con un archivo setup.py, aquí te dejo el enlace del módulo http://wwwsearch.sourceforge.net/mechanize/download.html