1

library(rvest)
library(dplyr)
library(stringr)

# 1. Path to your saved HTML file
file_path <- "C:/Users/FU/Downloads/C_public.htm"

# 2. Read lines and convert encoding safely
html_lines <- readLines(file_path, warn = FALSE)
html_text <- paste(html_lines, collapse = "\n")
html_text_utf8 <- iconv(html_text, from = "Big5", to = "UTF-8")

# Parse into rvest
page <- read_html(html_text_utf8)

# 3. FIX: Extract the correct table at index 2
tables <- html_table(page, fill = TRUE)
df_twse <- tables[[2]]

# 4. Set headers and remove the first row
names(df_twse) <- as.character(df_twse[1, ])
df_twse <- df_twse[-1, ]

# 5. Clean and filter the data
df_twse_clean <- df_twse %>%
  rename(code_and_name = 1, listing_date = 3, industry = 5) %>%
  filter(str_detect(code_and_name, "^\\d{4}\\s+")) %>%
  mutate(
    stock_id = str_extract(code_and_name, "^\\d{4}"),
    name = str_trim(str_replace(code_and_name, "^\\d{4}\\s+", ""))
  ) %>%
  select(stock_id, name, listing_date, industry) %>%
  filter(!str_detect(industry, "ETF|存託憑證|受益證券"))

head(df_twse_clean)
## # 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   水泥工業

2

Required Packages and Endpoint Setup

library(httr)
library(jsonlite)
library(dplyr)
library(stringr)

# Define API parameters
url <- "https://api.finmindtrade.com/api/v4/data"
query_params <- list(
  dataset = "TaiwanStockInfo"
)

Fetching and Parsing the Data

response <- GET(url, query = query_params)
json_text <- content(response, as = "text", encoding = "UTF-8")
data_list <- fromJSON(json_text)
df_finmind <- data_list$data

Clean and filter the FinMind data

library(dplyr)
library(stringr)

df_finmind_clean <- df_finmind %>%
  filter(type == "twse") %>%
  filter(str_detect(stock_id, "^\\d{4}$")) %>%
  filter(!str_detect(industry_category, "ETF|存託憑證|受益證券") & 
         !str_detect(stock_name, "ETF|存託憑證|受益證券")) %>%

  select(
    stock_id,
    name = stock_name,
    listing_date = date,
    industry = industry_category
  )

head(df_finmind_clean)
##   stock_id   name listing_date     industry
## 1     2241 艾姆勒   2021-06-01     電機機械
## 2     1443   立益   2021-06-01     紡織纖維
## 3     3669   圓展   2021-06-01       光電業
## 4     6165   浪凡   2021-06-01     電子工業
## 5     6165   浪凡   2021-06-01 電子零組件業
## 6     2429 銘旺科   2021-06-01 電子零組件業