-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
86 lines (51 loc) · 2.26 KB
/
main.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
import time
# --- 自訂函式 ---
from crawler_function import get_balance_sheet, get_income_statement, get_stock_numbers, get_financial_ratios
from file_function import save_datas, read_datas
from export_to_excel import export_income_to_excel
def company_info() -> None:
''' 取得 公司股票資料 並存成 txt 檔案 '''
number = get_stock_numbers()[0]
name = get_stock_numbers()[1]
save_datas(number, 'number', 'stock_number.txt')
save_datas(name, 'name', 'stock_name.txt')
return None
def income(seasion : str) -> None:
''' 取得 損益表資料 並存成 json 文件 (seasion = 20212 or 20211) '''
number_list = read_datas('number', 'stock_number.txt')
number_list = number_list[0].split('\n')
for number in number_list:
datas_list = get_income_statement(f'https://pchome.megatime.com.tw/stock/sto2/ock1/{seasion}/sid{number}.html')
print(datas_list)
print('-----------------------')
save_datas(datas_list, str(number), f'income_statement_{seasion}.json')
time.sleep(5)
print('Done!')
return None
def balance() -> None:
''' 取得 資產負債表 並存成 json 文件 '''
number_list = read_datas('number', 'stock_number.txt')
number_list = number_list[0].split('\n')
for number in number_list:
datas_list = get_balance_sheet(f'https://pchome.megatime.com.tw/stock/sto2/ock0/sid{number}.html')
print(datas_list)
print('-----------------------')
save_datas(datas_list, str(number), 'balance_sheet.json')
time.sleep(5)
print('Done!')
return None
def financial_ratios() -> None:
''' 取得 財務比率 的資料,並存成 txt 檔 '''
number_list = read_datas('number', 'stock_number.txt')
number_list = number_list[0].split('\n')
for number in number_list:
data_list = get_financial_ratios(f'https://pchome.megatime.com.tw/stock/sto2/ock2/sid{number}.html')
print(data_list)
print('------------------')
with open(f'DATA/{number}/financial_ratios.txt', 'w', encoding='utf-8') as f:
for data in data_list:
f.write(str(data) + '\n')
time.sleep(3)
print('Done!')
if __name__ == '__main__':
financial_ratios()