Last active
May 24, 2019 20:39
-
-
Save brenorb/f4bf7c5b1a4093acc97260e45d7aa26f to your computer and use it in GitHub Desktop.
Nubank expenses CSV maker
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from bs4 import BeautifulSoup as bs\n", | |
"import csv" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# nubank.txt contém somente a tabela feedTable com os gastos\n", | |
"with open(\"nubank.txt\") as f:\n", | |
" table = f.read()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"soup = bs(table, 'html.parser')\n", | |
"t = list(soup.children)[0]\n", | |
"dia = list(t.children)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{}" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# todas as datas com gastos\n", | |
"datas = [list(list(list(dia[i].children)[0].children)[0].children)[0] for i in range(len(dia))]\n", | |
"compras = {datas[i]:{} for i in range(len(datas))}\n", | |
"compras['30/Jan/2019']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"csv_data = []\n", | |
"for i in range(len(dia)):\n", | |
" item = list(dia[i].children)\n", | |
" for k in range(1,len(item)):\n", | |
" # exclui mensagens como \"sua fatura está paga\"\n", | |
" try:\n", | |
" valor = list(list(list(list(item[k].children)[0].children)[0].children)[4].children)[0]\n", | |
" except:\n", | |
" pass\n", | |
" else:\n", | |
" data = list(list(list(dia[i].children)[0].children)[0].children)[0]\n", | |
" categoria = list(list(list(list(item[k].children)[0].children)[0].children)[2].children)[0]\n", | |
" try:\n", | |
" quem = list(list(list(list(item[k].children)[0].children)[0].children)[3].children)[0]\n", | |
" except:\n", | |
" quem = ''\n", | |
" try:\n", | |
" tag = list(list(list(list(list(item[k].children)[0].children)[0].children)[5].children)[0].children)\n", | |
" except:\n", | |
" tag = ''\n", | |
" # descomente para checar se está tudo certo\n", | |
" # print(data, categoria, quem, valor, tag) \n", | |
" csv_data.append({'data':data,'fornecedor':quem,'valor':valor,'categoria':categoria,'tag':tag})\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"csv_columns = ['data', 'fornecedor', 'valor', 'categoria', 'tag']\n", | |
"csvfile = 'gastos_nubank.csv'\n", | |
"try:\n", | |
" with open(csvfile, 'w') as csvfile:\n", | |
" writer = csv.DictWriter(csvfile, fieldnames=csv_columns)\n", | |
" writer.writeheader()\n", | |
" for data in csv_data:\n", | |
" writer.writerow(data)\n", | |
"except IOError:\n", | |
" print(\"I/O error\") " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<tr class=\"dc-table-group\"><td class=\"dc-table-label\" colspan=\"1\">28/Jan/2019</td></tr>\n", | |
"--------------------------\n", | |
"<tr class=\"dc-table-row info\"><td class=\"dc-table-column _0\"><div class=\"event-card transaction card_present\" data-link=\"https://prod-s0-webapp-proxy.nubank.com.br/api/proxy/AJxL5LB0fd3NJPMqRx45zey0RlsWVT3NoA.aHR0cHM6Ly9wcm9kLXMwLWZlZWQubnViYW5rLmNvbS5ici9hcGkvdHJhbnNhY3Rpb25zLzVjNGYyMmYxLTYyOTAtNDE5ZC1hMDQ1LTU0ZTJmYTllNDQ4Mw\" id=\"5c4f22f1-6290-419d-a045-54e2fa9e4483\"><div class=\"type\"></div> <span class=\"title\">outros</span><h4 class=\"description\">Mega Loja</h4><div class=\"amount\">R$ 27,99</div><span class=\"tags\"></span> <span class=\"time\">28 Jan</span> </div><div class=\"hover\"></div></td></tr>\n", | |
"--------------------------\n", | |
"23/Mai/2019\n", | |
"--------------------------\n", | |
"outros\n" | |
] | |
} | |
], | |
"source": [ | |
"# Rascunho para poder ler e entender o que está no código\n", | |
"for i in range(len(item)):\n", | |
" print(item[i])\n", | |
" print('--------------------------')\n", | |
" \n", | |
"print(list(list(list(dia[0].children)[0].children)[0].children)[0])\n", | |
"print('--------------------------')\n", | |
"print(list(list(list(list(item[1].children)[0].children)[0].children)[2].children)[0])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment