Last active
January 20, 2017 01:24
-
-
Save juanescalona/adc82e0a7c42fc039eea697093f7063a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Descargar archivos.xml desde http://www.servel.com/archivos.xml | |
import xml.etree.ElementTree as ET | |
import urllib2, os, sys | |
def progress(nregion, comuna, download, response, chunk_size=8192, report_hook=None): | |
total_size = float(response.headers["Content-Length"]) | |
bytes_size = 0.0 | |
data = [] | |
while 1: | |
chunk = response.read(chunk_size) | |
bytes_size += len(chunk) | |
if not chunk: | |
break | |
data += chunk | |
if report_hook: | |
report_hook(nregion, comuna, download, bytes_size, chunk_size, total_size) | |
return "".join(data) | |
def report(nregion, comuna, download, bytes_size, chunk_size, total_size): | |
percent = float(bytes_size) / total_size | |
percent = round(percent*100, 2) | |
sys.stdout.write("Descargando %s - %s - %s %0.2f de %0.2f mb (%0.2f%%)\r" % (nregion, comuna, download, float(bytes_size/1000000), float(total_size/1000000), percent)) | |
if bytes_size >= total_size: | |
sys.stdout.write('\n') | |
if __name__ == '__main__': | |
tree = ET.parse('archivos.xml') | |
root = tree.getroot() | |
url = 'http://www.servel.cl/padron/' | |
region = root.findall('Region') | |
for s in region: | |
idregion = s[0].text | |
nregion = s[1].text | |
for node in s.findall('comunas'): | |
for c in node.findall('comuna'): | |
comuna = c[0].text | |
comuna.replace(" ","_") | |
archcomuna = c[2].text | |
download = url + archcomuna | |
try: | |
opener = urllib2.build_opener() | |
opener.addheaders = [('User-Agent', 'Mozilla/5.0')] | |
response = opener.open(download) | |
folder = nregion.strip().replace(" ", "-") | |
if not os.path.exists(folder): | |
os.makedirs(folder) | |
pdf = open(folder +"/"+ comuna +".pdf",'wb') | |
pdf.write(progress(nregion, comuna, download, response, report_hook=report)) | |
pdf.close() | |
except urllib2.HTTPError, error: | |
print error.read() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Se podría descargar automáticamente archivos.xml