Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(pipeR)
- library(httr)
- library(xml2)
- library(rvest)
- library(stringi)
- library(stringr)
- # windows才需要先把locale改成C,並用stringi::stri_conv轉成big5
- if (Sys.info()["sysname"] == "Windows") {
- backupLocale <- Sys.getlocale("LC_COLLATE")
- Sys.setlocale("LC_ALL", 'C')
- }
- url <- "http://www.lotto-8.com/listlto539.asp?indexpage=%i&orderby=new"
- maxPage <- GET(sprintf(url, 1)) %>>% content %>>% xml_find_all("//td/a[@target][3]") %>>%
- xml_attr("href") %>>% str_extract("=\\d+") %>>% str_sub(2L) %>>% as.integer
- out <- lapply(1:maxPage, function(i){
- tmp <- GET(sprintf(url, i)) %>>% content %>>%
- xml_find_first("//table[@class='auto-style4']") %>>%
- html_table(header = TRUE)
- if (Sys.info()["sysname"] == "Windows")
- names(tmp) <- stri_conv(names(tmp), "UTF-8", "big5")
- tmp[[2]] <- iconv(tmp[[2]], "latin1", "ASCII", sub="")
- return(tmp[1:2])
- }) %>>% do.call(what = rbind)
- if (Sys.info()["sysname"] == "Windows") {
- Sys.setlocale(locale = backupLocale)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement