Last active
September 8, 2016 09:51
-
-
Save c3h3/e9233c2b6f8c9634de0d8f075845e91a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(httr) | |
library(rvest) | |
library(XML) | |
library(testit) | |
library(magrittr) | |
# 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=") | |
url %>% GET %>% content(as = "text",encoding = "utf8") %>% `Encoding<-`("UTF-8") %>% xmlToDataFrame | |
} | |
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") | |
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) | |
# tables[[8]] %>% View | |
data_table_filter = sapply(tables, function(tab){ | |
tab[1,1] == "序號" | |
}) | |
tables[data_table_filter][[1]] | |
} | |
df = getFlightsData(fareType="Y") | |
df %>% View |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment