celestialgod

familyMart location

Jun 21st, 2016
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.25 KB | None | 0 0
  1. library(httr)
  2. library(xml2)
  3. library(pipeR)
  4. library(purrr)
  5. library(stringi)
  6. library(stringr)
  7. library(RCurl)
  8. library(jsonlite)
  9.  
  10. url_postCodes <- "http://www.easytravel.com.tw/postid_search.asp"
  11. all_postCodes <- url_postCodes %>>% GET %>>% content(encoding = "big5") %>>%
  12.   xml_find_all("//td/font") %>>% xml_text %>>%
  13.   stri_conv(from = "UTF-8", to = "Big5") %>>% str_extract("\\d{3}") %>>%
  14.   `[`(!is.na(.)) %>>% as.integer
  15.  
  16. # method with RCurl::getURL
  17. familyMartLoc <- lapply(all_postCodes, function(postCode){
  18.   url_toPrint <- str_c("http://api.map.com.tw/net/GraphicsXY.aspx?",
  19.                        "search_class=Zip&Zip=%i&fun=getCityByZipReturn")
  20.   city_area <- sprintf(url_toPrint, postCode) %>>%
  21.     getURL(referer = "http://www.family.com.tw/marketing/inquiry.aspx") %>>%
  22.     str_replace_all("getCityByZipReturn\\(|\\)", "") %>>% fromJSON
  23.  
  24.   url_toPrint <- str_c("http://api.map.com.tw/net/familyShop.aspx?",
  25.                        "searchType=ShopList&type=&city=%s&area=%s&road=&",
  26.                        "fun=showStoreList&key=6F30E8BF706D653965BDE302661D1241F8BE9EBC")
  27.   sprintf(url_toPrint, city_area$COUNTY, city_area$TOWN) %>>%
  28.     getURL(referer = "http://www.family.com.tw/marketing/inquiry.aspx") %>>%
  29.     str_replace_all("showStoreList\\(|\\)", "") %>>% fromJSON
  30. }) %>>% do.call(what = rbind)
  31.  
  32. # method with httr::GET
  33. familyMartLoc2 <- lapply(all_postCodes, function(postCode){
  34.   url_toPrint <- str_c("http://api.map.com.tw/net/GraphicsXY.aspx?",
  35.                        "search_class=Zip&Zip=%i&fun=getCityByZipReturn")
  36.   city_area <- sprintf(url_toPrint, postCode) %>>%
  37.     GET(config(referer = "http://www.family.com.tw/marketing/inquiry.aspx")) %>>%
  38.     content("text") %>>% str_replace_all("getCityByZipReturn\\(|\\)", "") %>>% fromJSON
  39.  
  40.   url_toPrint <- str_c("http://api.map.com.tw/net/familyShop.aspx?",
  41.                        "searchType=ShopList&type=&city=%s&area=%s&road=&",
  42.                        "fun=showStoreList&key=6F30E8BF706D653965BDE302661D1241F8BE9EBC")
  43.   sprintf(url_toPrint, city_area$COUNTY, city_area$TOWN) %>>%
  44.     GET(config(referer = "http://www.family.com.tw/marketing/inquiry.aspx")) %>>%
  45.     content("text") %>>% str_replace_all("showStoreList\\(|\\)", "") %>>% fromJSON
  46. }) %>>% do.call(what = rbind)
Advertisement
Add Comment
Please, Sign In to add comment