-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
4,454 additions
and
95 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
# Ignore output of scraper | ||
data.sqlite | ||
scraperwiki.sqlite | ||
scraperwiki.sqlite-journal | ||
data.sqlite-journal | ||
downloads/* | ||
dados.csv | ||
downloads/ | ||
bases/ | ||
bases/* |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"cells": [], | ||
"metadata": {}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"cells": [], | ||
"metadata": {}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import print_function, with_statement | ||
|
||
import os | ||
import time | ||
from datetime import datetime | ||
|
||
import pandas as pd | ||
import scraperwiki | ||
from chardet.universaldetector import UniversalDetector | ||
|
||
import utils | ||
|
||
|
||
def main(): | ||
# morph.io requires this db filename, but scraperwiki doesn't nicely | ||
# expose a way to alter this. So we'll fiddle our environment ourselves | ||
# before our pipeline modules load. | ||
os.environ['SCRAPERWIKI_DATABASE_NAME'] = 'sqlite:///data.sqlite' | ||
|
||
file_path = os.path.join("dados.csv") | ||
df = pd.read_csv( | ||
file_path, | ||
skiprows=2, | ||
encoding='iso-8859-1', | ||
sep='\t' | ||
) | ||
|
||
df = df.rename(columns={ | ||
u'Data': "dt_referencia", | ||
u'Emissor': "emissor", | ||
u'C\xf3digo do Ativo': "co_ativo", | ||
u'ISIN': "isin", | ||
u'Quantidade': "quantidade", | ||
u'N\xfamero de Neg\xf3cios': "nu_negocios", | ||
u'PU M\xednimo': "pu_minimo", | ||
u'PU M\xe9dio': "pu_medio", | ||
u'PU M\xe1ximo': "pu_maximo", | ||
u'% PU da Curva': "pu_curva" | ||
}) | ||
|
||
# converte para datetime | ||
df['dt_referencia'] = pd.to_datetime( | ||
df['dt_referencia'], format='%d/%m/%Y').dt.date | ||
|
||
# formata o campo para float | ||
df['pu_medio'] = df['pu_medio'].str.replace('.', '') | ||
df['pu_medio'] = df['pu_medio'].str.replace(',', '.') | ||
df['pu_medio'] = pd.to_numeric(df['pu_medio'], errors='coerce') | ||
|
||
# formata o campo para float | ||
df['pu_minimo'] = df['pu_minimo'].str.replace('.', '') | ||
df['pu_minimo'] = df['pu_minimo'].str.replace(',', '.') | ||
df['pu_minimo'] = pd.to_numeric(df['pu_minimo'], errors='coerce') | ||
|
||
# formata o campo para float | ||
df['pu_maximo'] = df['pu_maximo'].str.replace('.', '') | ||
df['pu_maximo'] = df['pu_maximo'].str.replace(',', '.') | ||
df['pu_maximo'] = pd.to_numeric(df['pu_maximo'], errors='coerce') | ||
|
||
# formata o campo para float | ||
df['pu_curva'] = df['pu_curva'].str.replace('.', '') | ||
df['pu_curva'] = df['pu_curva'].str.replace(',', '.') | ||
df['pu_curva'] = pd.to_numeric(df['pu_curva'], errors='coerce') | ||
|
||
# salva o arquivo de saída | ||
file_path = os.path.join('bases', 'debentures_negociacao.csv') | ||
print('Salvando resultado capturado no arquivo', file_path) | ||
df.to_csv(file_path, mode='a', index=False) | ||
|
||
print('Importing {} items'.format(len(df))) | ||
|
||
for index, row in enumerate(df.to_dict('records')): | ||
print(f'{index+1} de {len(df)}') | ||
scraperwiki.sqlite.save( | ||
unique_keys=['dt_referencia', 'co_ativo'], data=row) | ||
|
||
print('{} Registros importados com sucesso'.format(len(df))) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() | ||
time.sleep(60) | ||
# rename file | ||
os.rename('scraperwiki.sqlite', 'data.sqlite') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
python-3.6.2 | ||
python-3.6.7 |
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
Oops, something went wrong.