draft/getChimei.R

getChimei <- function(){
  #查詢分頁連結
  res <- GET("http://www.chimei.com.tw/service/product-support/stores/lcd-display-tv/")
  res <- htmlParse(content(res,'text', encoding = 'utf8'), encoding = 'utf8')
  
  page_list=cssApply(res,'#content > div.dropdown > p > select > option:nth-child(2)',cssValue)
  page_list=page_list[-1]
  wantURL <- sprintf('http://www.chimei.com.tw%s',page_list)
  page_num=length(wantURL)
  #產品名稱
  prod_name=substr(wantURL,regexpr("stores/",wantURL)[1]+7,100)
  prod_name <- str_replace_all(prod_name, '([/])+', '')
  
  OUTPUT=list()
  for(i in 1:page_num){
    res <- GET(wantURL[i])
    res <- htmlParse(content(res,'text', encoding = 'utf8'), encoding = 'utf8')
    type=xpathSApply(res, '//*[@class="clearfix"]/table/tbody/tr/td[1]', xmlValue)
    store_nm=xpathSApply(res, '//*[@class="clearfix"]/table/tbody/tr/td[2]', xmlValue)
    store_nm <- str_replace_all(store_nm, '(\r|\n| )+', '')
    addr=xpathSApply(res, '//*[@class="clearfix"]/table/tbody/tr/td[3]', xmlValue)
    addr <- str_replace_all(addr, '(\r|\n| |\t)+', '')
    addr <- str_replace_all(addr, '([(]).+$', '')
    addr <- str_replace_all(addr, '([(]).+$', '')
    tel_no=xpathSApply(res, '//*[@class="clearfix"]/table/tbody/tr/td[5]', xmlValue)
    if (length(tel_no)>0) {
      OUTPUT[i]= list(data.frame(brand_nm='奇美家電',prod_nm=prod_name[i],type=type,store_nm=store_nm,addr=addr,tel_no=tel_no, data_dt=gsub('-','',Sys.Date()) ,stringsAsFactors=FALSE ))
    }
  }
  FINISH=do.call(rbind,OUTPUT)
  return(FINISH)
}
leoluyi/CRMaddress documentation built on May 21, 2019, 5:08 a.m.