draft/getFormosaTimes.R

getFormosaTimes <- function(){
  URL="http://www.formosatimes.com.tw/js/area.js"
  res5 <- GET(URL, encoding='utf8')
  res6 <- content(res5, "text", encoding = "utf8")
  res6<-str_replace_all(res6,'[:space:]',' ')
  
  index_no<-unlist(str_extract(res6,"index\\[.+//\\$\\('#s_"))
  index_no2<-unlist(strsplit(index_no,';'))
  index_no2<-unlist(str_extract_all(index_no2,'index.+'))
  index_no3<-unlist(str_extract_all(index_no2,'[0-9]+$'))
  
  URL2=sprintf('http://www.formosatimes.com.tw/get_retail_l.php?s_zip=%s',index_no3)
  
  getforinfo<-function(URL){
    res <- GET(URL, encoding='utf8')
    res2 <- content(res, "text", encoding = "utf8")
    res3<-sub("[^\\]]*$","",sub("^[^\\[]*","",res2))
    res4<-rjson::fromJSON(res3)
    res4<-unlist(res4)
    if(is.null(res4)==TRUE){
    }else
    {
      store_info <-matrix(data=res4,ncol = 7,byrow = TRUE)
      addr <- paste0(store_info[,3],store_info[,4],store_info[,6])
      store_nm<-store_info[,2]
      tel_no<-store_info[,7]
      result <- data.frame(brand_nm='寶島鐘錶',store_nm=store_nm, addr=addr, tel_no=tel_no,
                           stringsAsFactors=FALSE )
    }
  }
  
  forinfo1<-lapply(URL2,getforinfo)
  forinfo2 <- do.call(rbind, forinfo1)
}
leoluyi/CRMaddress documentation built on May 21, 2019, 5:08 a.m.