url <- "https://isin.twse.com.tw/isin/C_public.jsp?strMode=2"
response <- GET(url)
stop_for_status(response)
raw_html <- content(response, "raw")
html_utf8 <- iconv(
rawToChar(raw_html),
from = "CP950",
to = "UTF-8",
sub = "byte"
)
page <- read_html(html_utf8, encoding = "UTF-8")
tables <- html_elements(page, "table") |>
html_table(fill = TRUE)
raw_twse <- tables[[which.max(sapply(tables, nrow))]]
twse_stocks <- raw_twse[-c(1, 2), ] |>
transmute(
stock_id = str_extract(X1, "\\d{4,6}"),
name = str_trim(str_remove(X1, "^\\d{4,6}\\s*")),
listing_date = X3,
industry = X5
) |>
filter(
!is.na(stock_id),
!is.na(name),
listing_date != "",
industry != ""
)
head(twse_stocks)
## # A tibble: 6 × 4
## stock_id name listing_date industry
## <chr> <chr> <chr> <chr>
## 1 1101 台泥 1962/02/09 水泥工業
## 2 1102 亞泥 1962/06/08 水泥工業
## 3 1103 嘉泥 1969/11/14 水泥工業
## 4 1104 環泥 1971/02/01 水泥工業
## 5 1108 幸福 1990/06/06 水泥工業
## 6 1109 信大 1991/12/05 水泥工業
nrow(twse_stocks)
## [1] 1110
finmind_url <- "https://api.finmindtrade.com/api/v4/data"
finmind_response <- GET(
finmind_url,
query = list(dataset = "TaiwanStockInfo")
)
stop_for_status(finmind_response)
finmind_json <- content(
finmind_response,
as = "parsed",
type = "application/json"
)
finmind_stocks <- bind_rows(finmind_json$data)
finmind_stocks_clean <- finmind_stocks |>
filter(
!grepl("ETF|基金|債券|特別股|認購權證|認售權證",
industry_category),
!grepl("ETF|基金|債券|特別股",
stock_name)
) |>
distinct(stock_id, .keep_all = TRUE) |>
transmute(
stock_id = stock_id,
name = stock_name,
industry = industry_category,
type = type
)
finmind_final <- finmind_stocks_clean |>
select(stock_id, name, industry, type)
head(finmind_final)
## # A tibble: 6 × 4
## stock_id name industry type
## <chr> <chr> <chr> <chr>
## 1 5481 新華 電子零組件業 tpex
## 2 5450 寶聯通 電腦及週邊設備業 tpex
## 3 3629 地心引力 光電業 tpex
## 4 3687 歐買尬 文化創意業 tpex
## 5 6238 勝麗 其他電子類 tpex
## 6 6497 亞獅康-KY 生技醫療業 tpex
nrow(finmind_final)
## [1] 2729
table(finmind_final$type)
##
## emerging tpex twse
## 545 913 1271