Advertisement
celestialgod

lottery crawler

Feb 19th, 2018
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.00 KB | None | 0 0
  1. library(pipeR)
  2. library(httr)
  3. library(xml2)
  4. library(rvest)
  5. library(stringi)
  6. library(stringr)
  7.  
  8. # windows才需要先把locale改成C,並用stringi::stri_conv轉成big5
  9. if (Sys.info()["sysname"] == "Windows") {
  10.   backupLocale <- Sys.getlocale("LC_COLLATE")
  11.   Sys.setlocale("LC_ALL", 'C')
  12. }
  13.  
  14. url <- "http://www.lotto-8.com/listlto539.asp?indexpage=%i&orderby=new"
  15. maxPage <- GET(sprintf(url, 1)) %>>% content %>>% xml_find_all("//td/a[@target][3]") %>>%
  16.   xml_attr("href") %>>% str_extract("=\\d+") %>>% str_sub(2L) %>>% as.integer
  17.  
  18. out <- lapply(1:maxPage, function(i){
  19.   tmp <- GET(sprintf(url, i)) %>>% content %>>%
  20.     xml_find_first("//table[@class='auto-style4']") %>>%
  21.     html_table(header = TRUE)
  22.   if (Sys.info()["sysname"] == "Windows")
  23.     names(tmp) <- stri_conv(names(tmp), "UTF-8", "big5")
  24.   tmp[[2]] <- iconv(tmp[[2]], "latin1", "ASCII", sub="")
  25.   return(tmp[1:2])
  26. }) %>>% do.call(what = rbind)
  27.  
  28. if (Sys.info()["sysname"] == "Windows") {
  29.   Sys.setlocale(locale = backupLocale)
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement