From 8285fdd5c6f89cbd5932dd8d95d5220c1855f998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20C=2E=20Barrionuevo=20da=20Luz?= Date: Mon, 25 Nov 2019 23:58:43 -0300 Subject: [PATCH] Melhorias em empresas_generator.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove o suporte a Python2 - Explicitamente força a utilização do encoding `uft-8` --- empresas_generator.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/empresas_generator.py b/empresas_generator.py index f534590f..a865cce9 100644 --- a/empresas_generator.py +++ b/empresas_generator.py @@ -1,17 +1,16 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # import json import os +import sys +from urllib.request import urlopen try: - # python 2 - from urllib2 import urlopen + from slugify import slugify except ImportError: - # python 3 - from urllib.request import urlopen - -from slugify import slugify + print("Este programa requer python-slugify. Por favor, instale-o usando:\npython3 -m pip install python-slugify\n") + sys.exit(1) # Diretório onde serão gerados os arquivos JSON @@ -23,7 +22,7 @@ def scrapping_empresas(): file = urlopen(EMPRESAS_FILE) - file = file.read().decode() + file = file.read().decode(encoding='utf-8') region = state = city = '' empresas = [] @@ -53,13 +52,18 @@ def scrapping_empresas(): return empresas -if __name__ == '__main__': +def main(): for empresa in scrapping_empresas(): filename = '{0}-{1}.json'.format( slugify(empresa['nome']), slugify(empresa['cidade'])) if not os.path.exists(PAGE_PATH): os.makedirs(PAGE_PATH) - - with open(os.path.join(PAGE_PATH, filename), 'w') as file: + file_path = os.path.join(PAGE_PATH, filename) + with open(file_path, mode='w', encoding='utf-8') as file: json.dump(empresa, file) + print("Gerado: {}".format(file_path)) + +if __name__ == '__main__': + main() +