Skip to content

Instantly share code, notes, and snippets.

@wush978
Last active November 24, 2018 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wush978/1619ddb3ed093a11febb0da592f5fc9d to your computer and use it in GitHub Desktop.
Save wush978/1619ddb3ed093a11febb0da592f5fc9d to your computer and use it in GitHub Desktop.
library(magrittr)
areas <- sprintf(
"https://www.cec.gov.tw/pc/zh_TW/TC/n6300000%02d00000000.html", 1:12
)
names(areas) <- "松山信義大安中山中正大同萬華文山南港內湖士林北投" %>%
strsplit(split = "") %>%
extract2(1) %>%
split(f = rep(1:12, each = 2)) %>%
sapply(paste, collapse = "")
root <- tempfile()
dir.create(root)
lapply(1:12, function(i) {
download.file(areas[i], destfile = file.path(root, sprintf("%02d.html", i)), quiet = TRUE)
}) %>%
invisible()
library(XML)
ding <- integer(12)
yao <- integer(12)
ko <- integer(12)
done <- integer(12)
total <- integer(12)
for(i in 1:12) {
. <- XML::readHTMLTable(file.path(root, sprintf("%02d.html", i)))
df <- .[[5]]
ding[i] <- gsub(",", "", df$V5[3]) %>% as.integer()
yao[i] <- gsub(",", "", df$V5[4]) %>% as.integer()
ko[i] <- gsub(",", "", df$V5[5]) %>% as.integer()
progress <- df$V1[7] %>% as.character() %>%
strsplit(split = "投開票所數 已送/應送: ") %>%
extract2(1) %>%
extract(2) %>%
strsplit(split = "/") %>%
extract2(1)
progress <- regmatches(progress, regexec("(\\d+)", progress)) %>%
sapply("[", 1) %>%
as.integer()
done[i] <- progress[1]
total[i] <- progress[2]
}
progress.txt <- sprintf("\t%s: 丁:%d 柯:%d %d/%d\n", names(areas), ding, ko, done, total)
progress.txt <- c(
sprintf("\t台北市: 丁:%d 柯:%d %d/%d\n", sum(ding), sum(ko), sum(done), sum(total)),
progress.txt
)
progress.txt %<>% paste(collapse = "")
sprintf(
"外差估計 丁:%0.1f 姚:%0.1f 柯:%0.1f 柯丁差距(柯-丁) ***%0.1f*** 總進度: %d/%d\n\n\n目前進度:\n%s\n",
sum(ding / done * total),
sum(yao / done * total),
sum(ko / done * total),
sum(ko / done * total) - sum(ding / done * total),
sum(done),
sum(total),
progress.txt
) %>%
cat()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment