-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsolidateStocks.py
43 lines (37 loc) · 1.19 KB
/
consolidateStocks.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
import os
from pyquery import PyQuery as pq
import lxml, lxml.html
import datetime
##carrega os papeis de stocksList.csv
stocksList = open('stocksList.csv', 'r')
papeis = stocksList.read().split(';')
stocksList.close()
## pegando o dia e mês
now = datetime.datetime.now()
dia = now.day-1
mes = now.month
data = str(dia)+"."+str(mes)
current_dir = os.path.dirname(__file__)
rel_path = data
for papel in papeis:
try:
#abrindo o arquivo do papel.html
arquivo = os.path.join(current_dir, rel_path+"\\"+papel+'.html')
print(arquivo)
##fazendo o scrap no arquivo do papel.html
conteudo = pq(filename=arquivo)
resultado = conteudo("td.data span")
print(len(resultado))
#escrevendo os resultados em data.mes
file = open(data+'.html','ab')
##file.write(bytes("<tr>", 'utf-8'))
for i in resultado:
conteudo = lxml.html.tostring(i)
##file.write(bytes("@", 'utf-8'))
file.write(conteudo)
file.write(bytes("@", 'utf-8'))
##file.write(bytes("</tr>", 'utf-8'))
file.write(bytes("<br/>", 'utf-8'))
file.close()
except:
print("Erro: "+papel)