-
Notifications
You must be signed in to change notification settings - Fork 5
/
scraper.py
144 lines (106 loc) · 4.22 KB
/
scraper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
#%%
## The MIT License (MIT)
##
## Copyright (c) 2015 Vinícius Tabille Manjabosco
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included in all
## copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## SOFTWARE.
#%%
import os
import itertools as it
import json
import scraperwiki
import mechanize
#%%
## Consts
JOGADORES_URL = 'http://cartolafc.globo.com/mercado/filtrar.json?page='
LOGIN_URL = 'https://loginfree.globo.com/login/438'
LOGIN_EMAIL = os.environ['MORPH_LOGIN_EMAIL']
LOGIN_SENHA = os.environ['MORPH_LOGIN_SENHA']
#%%
## Download dados
print '[LOG] Download Iniciado'
# Inicialisa browser
br = mechanize.Browser()
# Abre a página de login Cartola através do mechanize
br.open(LOGIN_URL)
# Na página, selecionamos o primeiro form presente. 'nr=0' indica que estamos
# selecionando o form de índice 0 dentre os encontrados.
# Analisando a página, vemos que realmente só há um form.
# Após selecionarmos o form, preenchemos os campos com username e
# senha que permitam fazer o login.
br.select_form(nr=0)
br.form['login-passaporte'] = LOGIN_EMAIL
br.form['senha-passaporte'] = LOGIN_SENHA
br.submit()
jsonRaw = []
for i in it.count(1):
url = JOGADORES_URL + str(i)
r = br.open(url)
j = json.loads(r.read())
jsonRaw.append(j)
pgAtual = int(j['page']['atual'])
pgTotal = int(j['page']['total'])
print '[LOG] Baixando ', i, '/', pgTotal
if pgAtual == pgTotal:
break
print '[LOG] Download Terminado'
#%%
## Minera Scouts
## e concatena em um DataFrame
print '[LOG] Processando dados'
# Concatena lista de atletas dos arquivos
atletasJSON = [j['atleta'] for j in jsonRaw]
atletasJSON = list(it.chain(*atletasJSON))
## Minera Rodada
rodada = jsonRaw[0]['rodada_id'] - 1
# Scouts
ScoutsDict = []
for atleta in atletasJSON:
scoutDict = {s['nome']:s['quantidade'] for s in atleta['scout']} # Add Scouts
scoutDict['Rodada'] = rodada
scoutDict['Atleta'] = atleta['id']
scoutDict['Apelido'] = atleta['apelido']
scoutDict['Clube'] = atleta['clube']['abreviacao']
scoutDict['Posicao'] = atleta['posicao']['abreviacao']
scoutDict['Status'] = atleta['status']
scoutDict['Pontos'] = float(atleta['pontos'])
scoutDict['PontosMedia'] = float(atleta['media'])
scoutDict['Preco'] = float(atleta['preco'])
scoutDict['PrecoVariacao'] = float(atleta['variacao'])
scoutDict['Mando'] = atleta['clube']['id'] == atleta['partida_clube_visitante']['id']
scoutDict['PartidaCasa'] = atleta['partida_clube_casa']['abreviacao']
scoutDict['PartidaVisitante'] = atleta['partida_clube_visitante']['abreviacao']
scoutDict['PartidaData'] = atleta['partida_data']
ScoutsDict.append(scoutDict)
print '[LOG] Processamento de dados terminado'
#%%
## Salva dados para SQLite
print '[LOG] Salvando dados'
scraperwiki.sqlite.save(unique_keys = ['Atleta', 'Rodada'],
data = ScoutsDict,
table_name = 'data')
#%%
## Atualiza Status de cada Atleta
print '[LOG] Atualizando Status'
SCOUTS_UPDATE_STATUS_QUERY = 'UPDATE data SET Status = ? WHERE Rodada = ? AND Atleta = ?'
scraperwiki.sqlite.execute(SCOUTS_UPDATE_STATUS_QUERY,
[[s['Status'], s['Rodada'], s['Atleta']] for s in ScoutsDict])
print '[LOG] Dados Salvos'