Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(httr)
- library(xml2)
- library(pipeR)
- library(purrr)
- library(stringi)
- library(stringr)
- library(RCurl)
- library(jsonlite)
- library(dplyr)
- url_postCodes <- "http://www.easytravel.com.tw/postid_search.asp"
- all_postCodes <- url_postCodes %>>% GET %>>% content(encoding = "big5") %>>%
- xml_find_all("//td/font") %>>% xml_text %>>%
- stri_conv(from = "UTF-8", to = "Big5") %>>% str_extract("\\d{3}") %>>%
- `[`(!is.na(.)) %>>% as.integer
- taiwanCityTown <- lapply(all_postCodes, function(postCode){
- url_toPrint <- str_c("http://api.map.com.tw/net/GraphicsXY.aspx?",
- "search_class=Zip&Zip=%i&fun=getCityByZipReturn")
- sprintf(url_toPrint, postCode) %>>%
- GET(config(referer = "http://www.family.com.tw/marketing/inquiry.aspx")) %>>%
- content("text") %>>% str_replace_all("getCityByZipReturn\\(|\\)", "") %>>% fromJSON
- }) %>>% do.call(what = rbind) %>>% filter(nchar(COUNTY) > 0, nchar(TOWN) > 0)
- sevenElevenStoreLoc <- taiwanCityTown %>>% as.matrix %>>% apply(1, function(v){
- url_toPrint <- str_c("https://emap.pcsc.com.tw/EMapSDK.aspx?",
- "commandid=SearchStore&city=%s&town=%s&ID=&StoreName=&",
- "SpecialStore_Kind=&isDining=False&isParking=False&isLavatory=False&",
- "isATM=False&is7WiFi=False&isIce=False&isHotDog=False&",
- "isHealthStations=False&isIceCream=False&isOpenStore=False&",
- "isFruit=False&isCityCafe=False&isUp=False&isOrganic=False&",
- "isCorn=False&isMakeUp=False&isMuji=False&isMD=False&",
- "isStarBucks=False&isIbon=False&isTea=False")
- storeLoc <- sprintf(url_toPrint, v[1], v[2]) %>>%
- GET(config(referer = "https://emap.pcsc.com.tw")) %>>%
- content("text") %>>% read_xml %>>% xml_find_all("GeoPosition")
- if (length(storeLoc) == 0)
- return(NULL)
- colNames <- storeLoc %>>% '[['(1) %>>% xml_children %>>% xml_name
- storeLoc %>>% xml_children %>>% xml_text %>>% stri_conv("UTF-8", "BIG5") %>>%
- matrix(length(colNames)) %>>% t %>>% data.frame(stringsAsFactors = FALSE) %>>%
- `names<-`(colNames)
- }) %>>% do.call(what = rbind) %>>%
- mutate_each(funs(str_replace_all(., "\\s", "")))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement