draft/getAptg.R

getAptg <- function(){
  URL='http://www.aptg.com.tw/corporate/retailStores.htm'
  res <- GET(URL, encoding='utf8')
  res2 <- htmlParse(content(res, "text", encoding = "utf8"), encoding = "utf8")
  city <- cssApply(res2,"#city > option",cssValue)
  city<-city[-1]
  wantURL <- sprintf('https://www.aptg.com.tw/ecare/Contact/jsonpOut.seam?command=jQuery181015780124836601317_1435414520479&cityId=%s',city[1:length(city)] )
  
  getDistAP <- function(URL){
    res<-GET(URL)
    res<-content(res)
    res<-sub("[^\\]]*$","",sub("^[^\\[]*","",res))
    res<-rjson::fromJSON(res)
    res2<-unlist(res)
    dist <-matrix(data=res2,ncol = 2,byrow = TRUE)
    dist<-dist[,1]
    return(dist)
  }
  getAddrAP <- function(dist){
    URL <- sprintf('https://www.aptg.com.tw/ecare/Contact/jsonpOut.seam?command=jQuery181015780124836601317_1435414520479&distId=%s',dist[1:length(dist)] )
    res<-GET(URL)
    res<-content(res)
    res<-sub("[^\\]]*$","",sub("^[^\\[]*","",res))
    res<-rjson::fromJSON(res)
    res2<-unlist(res)
    result<-list()
    if(length(res)==0) {
      result <- NULL
    }else {
      for(i in 1:length(res)){
        result[[i]]<-lapply(res[[i]],function(x) ifelse(is.null(x),NA,x))
      }
      result<-lapply(result,function(x) do.call(cbind,x))
      result<-do.call(rbind,result)
      Title = c("id","store_nm","address","storeHours","tel_no","email","mobile","type","sort","status","createDate","longitude","latitude","addr","mapLink")
      colnames(result)=Title
      brand_nm='亞太電信'
      result<-cbind(brand_nm,result)
    }
    return(result)
  }
  distList <- unlist(lapply(wantURL,getDistAP))
  addrList<-lapply(distList,getAddrAP)
  addrList2 <- do.call(rbind, addrList)
  return(as.data.frame(addrList2, stringsAsFactors = FALSE))
}
leoluyi/CRMaddress documentation built on May 21, 2019, 5:08 a.m.