-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStock.py
186 lines (161 loc) · 7.55 KB
/
Stock.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import re
import os
import csv
import requests
import pandas as pd
import yfinance as yf
from tabulate import tabulate
from bs4 import BeautifulSoup
class Get:
def __init__(self):
self.stock_id = None
self.stock_name = None
self.stock_type = None
def search(self, code):
try:
with open('csv/twse.csv', 'r') as twse:
reader = csv.reader(twse)
for row in reader:
if len(row) >= 2:
if code == row[0] or code == row[1]:
self.stock_id = row[0] + '.TW'
self.stock_name = row[1]
self.stock_type = row[5]
print('上櫃公司股票代碼:' + row[0] + ' 上市公司股票名稱:' + row[1] + ' 公司類型:' + row[5])
return {'stock_id': self.stock_id, 'name': self.stock_name,'type': self.stock_type}
with open('csv/tpex.csv', 'r') as tpex:
reader = csv.reader(tpex)
for row in reader:
if len(row) >= 2:
if code == row[0] or code == row[1]:
self.stock_id = row[0] + '.TWO'
self.stock_name = row[1]
self.stock_type = row[5]
print('上櫃公司股票代碼:' + row[0] + ' 上櫃公司股票名稱:' + row[1] + ' 公司類型:' + row[5])
return {'stock_id': self.stock_id, 'name': self.stock_name, 'type': self.stock_type}
return None
except:
if all('\u4e00' <= char <= '\u9fff' for char in code) or code.isdigit():
url = f'https://goodinfo.tw/tw/StockBzPerformance.asp?STOCK_ID={code}'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36'}
res = requests.get(url, headers=headers)
res.encoding = 'utf-8'
html = res.text
soup = BeautifulSoup(html, 'html.parser')
if '您的瀏覽量異常, 已影響網站速度, 目前暫時關閉服務' in html:
error = '暫時無法讀取資料'
return {'stock_id': error}
else:
for link in soup.find_all('a', class_='link_blue'):
if re.search('StockDetail\.asp\?STOCK_ID=(\d+)', link.get('href')):
stock_id = re.search('STOCK_ID=(\d+)', link.get('href')).group(1)
return {'stock_id': stock_id + '.TW'}
def price(self, code):
data = yf.download(code , period = '1d', rounding = True)
open = str(data.iloc[0]['Open'])
close = str(data.iloc[0]['Close'])
high = str(data.iloc[0]['High'])
low = str(data.iloc[0]['Low'])
return {'open': open, 'close': close, 'high': high, 'low': low}
def yf_fin(self, code):
try:
data = yf.Ticker(code)
pe = data.info['trailingPE']
pe_rounded = '{:.2f}'.format(pe)
eps = data.info['bookValue']
return {
'pe': str(pe_rounded),
'bv': str(eps),
}
except KeyError as e:
print(e)
return {
'pe': '查無本益比資訊',
'bv': '查無EPS資訊',
}
def goodinfo_fin(self, code, columns, years):
code = re.sub(r'\.(TW|TWO)$', '', code)
url = f'https://goodinfo.tw/tw/StockBzPerformance.asp?STOCK_ID={code}'
print(url)
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36'}
res = requests.get(url, headers=headers)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text, 'html.parser')
table = soup.find('div', {'id': 'txtFinDetailData'}).find('table')
rows = table.find_all('tr')
data = []
count = 0
for row in rows:
tds = row.find_all('td')
if len(tds) > 1: # Avoid None
values = []
for col in columns:
if col < len(tds):
value = tds[col].text.strip()
if not re.search('[\u4e00-\u9fff]', value):
values.append(value)
data.append(values)
count += 1
if count >= years:
break
df = pd.DataFrame(data, columns = values)
# First columns
mapping = {
0: '年度',
1: '股本(億)',
2: '財報評分',
3: '收盤',
4: '平均',
5: '漲跌',
6: '漲跌(%)',
7: '營業收入',
8: '營業毛利',
9: '營業利益',
10: '業外損益',
11: '稅後淨利',
12: '營業毛利(%)',
13: '營業利益(%)',
14: '業外損益(%)',
15: '稅後淨利(%)',
16: 'ROE(%)',
17: 'ROA(%)',
18: '稅後EPS',
19: '年增(元)',
20: 'BPS(元)'
}
labels = [mapping.get(col) for col in columns]
df.columns = labels
return df
def goodinfo_to_csv(self, code, args, rows):
url = f'https://goodinfo.tw/tw/StockBzPerformance.asp?STOCK_ID={code}'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36'}
res = requests.get(url, headers = headers)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text, 'html.parser')
table = soup.find('div', {'id': 'txtFinDetailData'}).find('table')
df = pd.read_html(str(table), header = None, skiprows = 1)[0]
df = df[~df.astype(str).apply(lambda row: row.str.contains('[\u4e00-\u9fff]').any(), axis=1)]
df.columns = df.columns.map(lambda x: x[0].replace(' ', '') if isinstance(x, tuple) else x.replace(' ', '')) # Remove spaces in column headers
# Add (%) to the second header
col_counts = df.columns.value_counts()
duplicated_col = col_counts[col_counts > 1].index # Find duplicate columns (ex: Index(['營業毛利', '...'], dtype='object'))
for columns in duplicated_col:
duplicated_indices = [i for i, col in enumerate(df.columns) if col == columns] # Find the indices of duplicate columns (ex: [8, 12])
for index in duplicated_indices[1:]:
df.columns.values[index] += '(%)'
df.to_csv(f'finance/{code}.csv', index = False)
print(f'{code}.csv 財報檔案已創建!')
return self.find_col(code, args, rows)
def find_col(self, code, args, rows):
df = pd.read_csv(f'finance/{code}.csv')
select_cols = df[list(args)].head(rows) # Find the specified columns
table = tabulate(select_cols, headers='keys', tablefmt='psql')
print(table)
return table
def table(self, code, *args, rows: int):
code = re.sub(r'\.(TW|TWO)$', '', code)
file_path = f'finance/{code}.csv'
if os.path.isfile(file_path):
return self.find_col(code, args, rows)
else:
return self.goodinfo_to_csv(code, args, rows)