-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.py
56 lines (44 loc) · 1.28 KB
/
play.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
from crawl import *
from stocks import *
import csv
import time
import os
date = time.strftime('%Y%m', time.localtime(time.time()))
codes = stock_codes()
output = f"results/data{date}.csv"
def contains(filename, code):
if not os.path.isfile(filename):
return False
csv_file = csv.reader(open(filename, "r"), delimiter=",")
for row in csv_file:
if code == row[0]:
return True
return False
def append(filename, data):
mode = "a" if os.path.isfile(filename) else "w"
with open(filename, mode, encoding='utf-8-sig', newline='') as f:
wr = csv.writer(f)
wr.writerow(list(data.values()))
f.close()
count = 0
total = len(codes)
for idx, code in enumerate(codes):
count += 1
try:
if contains(output, code):
print(f'{count}/{total} ##### {code} pass')
continue
market = get_market(code)
print(market)
if market == 'KONEX':
print(f'{count}/{total} ##### {code} pass KONEX')
continue
data, rowData = crawl(code)
pp(data, width=30)
pp(rowData, width=30)
append(output, rowData)
print(f'{count}/{total} ##### {code} done')
except Exception as e:
print(e)
time.sleep(20)
time.sleep(2)