library(rvest) library(httr) library(dplyr) library(stringr)
url_twse <- “https://isin.twse.com.tw/isin/C_public.jsp?strMode=2” response_twse <- GET(url_twse) content_twse <- content(response_twse, as = “text”, encoding = “Big5”)
df_raw <- html_table(read_html(content_twse), fill = TRUE)[[1]]
colnames(df_raw) <- df_raw[1, ] df_twse_clean <- df_raw[-1, ] %>% rename(security = 1, listing_date = 3, industry = 5) %>% mutate( stock_id = str_extract(security, “1+”), name = str_trim(str_remove(security, “2+\s*“)) ) %>% filter( !is.na(stock_id), str_length(stock_id) == 4, !str_detect(name,”ETF|基金|受益證券|存託憑證”) ) %>% select(stock_id, name, listing_date, industry)
head(df_twse_clean)