draft/getLesEnphants.R

getLesEnphants <- function(){
  res <- GET("https://www.phland.com.tw/map_list.php")
  resText <- content(res, 'text', encoding = 'utf8')
  res <- content(res, encoding = 'utf8')
  idTag <- str_extract_all(resText,'function open_z[0-9]+.+.+#z[0-9]+.+;')[[1]]
  idTag <-  unlist(str_extract_all(idTag,'(?<=\\#)z[0-9]+'))
  
  wantLink <- list()
  for(i in 1:length(idTag)){
    wantLinkTp <- xpathSApply(res, sprintf('//*[@id="%s"]/div/ul/li/span/a',idTag[i]), xmlAttrs)
    wantLinkTp <- sprintf("https://www.phland.com.tw/%s", wantLinkTp)
    wantLink[[i]] <- wantLinkTp
  }
  wantLink <- unlist(wantLink)
  
  result<- list()
  for (i in 1:length(wantLink)){
    res <- GET(wantLink[i])
    res <- content(res, encoding = 'utf8')
    store_nm <- xpathSApply(res, '//*[@id="word"]/p/span', xmlValue)
    tel_no <- xpathSApply(res, '//*[@id="word"]/table/tr[2]/td[2]', xmlValue)
    open <- xpathSApply(res, '//*[@id="word"]/table/tr/td[4]', xmlValue)
    addr <- xpathSApply(res, '//*[@id="word"]/table/tr[3]/td[2]', xmlValue)
    note <- xpathSApply(res, '//*[@id="word"]/table/tr[4]/td[2]', xmlValue)
    result[[i]] <- data.frame(brand_nm='麗嬰房', store_nm, tel_no , open, addr, note, stringsAsFactors=FALSE)
  }
  result <- do.call(rbind, result)
  result$tel_no <- str_replace_all(result$tel_no, '\\(|\\)| |-', '')
  result$travelNccc <- ifelse(str_detect(result$note,'國民旅遊卡'),'Y','N')
  result$outlet <- ifelse(str_detect(result$note,'暢貨中心'),'Y','N')
  result
}
leoluyi/CRMaddress documentation built on May 21, 2019, 5:08 a.m.