Skip to content

Instantly share code, notes, and snippets.

@whizzalan
Created September 8, 2016 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whizzalan/80057d9757baccfc02330d07b6403499 to your computer and use it in GitHub Desktop.
Save whizzalan/80057d9757baccfc02330d07b6403499 to your computer and use it in GitHub Desktop.
library(httr)
library(rvest)
library(XML)
library(testit)
library(magrittr)
library(dplyr)
# origin="TPE"
# destination="SEA"
getAirlineCode <- function(origin="TPE", destination="SEA") {
url = paste0("http://www.colatour.com.tw/C10C_AirTicket/C10C_00_Query.aspx?TxType=Airline&Destination=",destination,"&Origin=",origin,"&AirlineCode=")
dat <- url %>% GET %>% content(as = "text",encoding = "utf8") %>% `Encoding<-`("UTF-8") %>% xmlToDataFrame
dat %<>% mutate(AirlineName = iconv(AirlineName, "utf-8", "big5"))
dat
}
getAirlineCode("TPE","SFO")
getFlightsData <- function(fareType="C", # C=business class &Y=econemic class
startDate="2016/09/03",
endDate="2016/09/03",
journeyType="來回",
originCode="TN",
destinationCode="SEA",
bankName="全部") {
journeyType %<>% `Encoding<-`("UTF-8")
bankName %<>% `Encoding<-`("UTF-8")
Sys.setlocale(category = "LC_ALL", locale = "C")
url = paste0("http://www.colatour.com.tw/C10C_AirTicket/C10C_01_FareChoose.aspx?",
"FareType=",fareType,
"&JourneyType=",URLencode(URLencode(journeyType)),
"&OriginCode=",originCode,"&DestinationCode=",destinationCode,
"&ReturnCode=&ReturnName=&RouteSummary=",paste0(originCode,"-",destinationCode,"-",originCode),
"&AirlineCode=&StartDate=",startDate,"&EndDate=",endDate,
"&BankName=",URLencode(bankName),"&DirectFlightMark=False")
url
tables = url %>%
GET %>%
content(as = "text",encoding = "utf8") %>%
`Encoding<-`("UTF-8") %>%
read_html %>%
html_table(trim = F,fill = T)
Sys.setlocale(category = "LC_ALL", locale = "Chinese (Traditional)_Taiwan.950")
tables
# tables[[8]] %>% View
}
tb <- getFlightsData(fareType="Y")
data_table_filter = sapply(tb, function(tab){
tab[1,1] == "序號"
# print(tab[1,1])
})
data_table_filter
df <- tb[data_table_filter][[1]]
df %>% View
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment