This question follows an issue I had a few years ago which I posted here Send SQL string through POST with httr package in R.
The basic idea is to use R to download a zip file generated here https://www.transtats.bts.gov/DL_SelectFields.aspx?gnoyr_VQ=FIL&QO_fu146_anzr=Nv4 Pn44vr45.
When accessing the website above, we can select from a table, the variables we want, the year, and month (or all months), and press download. Before, when I right-clicked on the POST line and choose "Copy as cURL", I would get a really nice set of code which I could translate into this:
httr::POST(
url = "https://www.transtats.bts.gov/DownLoad_Table.asp",
httr::add_headers(
Referer = "https://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=289"
),
body = list(
UserTableName = "DB1BCoupon",
DBShortName = "",
RawDataTable = "T_DB1B_COUPON",
sqlstr = " SELECT ORIGIN_AIRPORT_ID,ORIGIN_AIRPORT_SEQ_ID,ORIGIN_CITY_MARKET_ID,DEST_AIRPORT_ID,DEST_AIRPORT_SEQ_ID,DEST_CITY_MARKET_ID FROM T_DB1B_COUPON WHERE Quarter=1 AND YEAR=2018",
varlist = "ORIGIN_AIRPORT_ID,ORIGIN_AIRPORT_SEQ_ID,ORIGIN_CITY_MARKET_ID,DEST_AIRPORT_ID,DEST_AIRPORT_SEQ_ID,DEST_CITY_MARKET_ID",
grouplist = "", suml = "",
sumRegion = "", filter1 = "title=",
filter2 = "title=", geo = "All\xa0",
time = "Q 1", timename = "Quarter",
GEOGRAPHY = "All", XYEAR = "2018",
FREQUENCY = "1",
VarDesc = "ItinID", VarType = "Num",
VarDesc = "MktID", VarType = "Num",
VarDesc = "SeqNum", VarType = "Num",
VarDesc = "Coupons", VarType = "Num",
VarDesc = "Year", VarType = "Num", VarName = "ORIGIN_AIRPORT_ID",
VarDesc = "OriginAirportID", VarType = "Num", VarName = "ORIGIN_AIRPORT_SEQ_ID",
VarDesc = "OriginAirportSeqID", VarType = "Num", VarName = "ORIGIN_CITY_MARKET_ID",
VarDesc = "OriginCityMarketID", VarType = "Num",
VarDesc = "Quarter", VarType = "Num",
VarDesc = "Origin", VarType = "Char",
VarDesc = "OriginCountry", VarType = "Char",
VarDesc = "OriginStateFips", VarType = "Char",
VarDesc = "OriginState", VarType = "Char",
VarDesc = "OriginStateName", VarType = "Char",
VarDesc = "OriginWac", VarType = "Num", VarName = "DEST_AIRPORT_ID",
VarDesc = "DestAirportID", VarType = "Num", VarName = "DEST_AIRPORT_SEQ_ID",
VarDesc = "DestAirportSeqID", VarType = "Num", VarName = "DEST_CITY_MARKET_ID",
VarDesc = "DestCityMarketID", VarType = "Num",
VarDesc = "Dest", VarType = "Char",
VarDesc = "DestCountry", VarType = "Char",
VarDesc = "DestStateFips", VarType = "Char",
VarDesc = "DestState", VarType = "Char",
VarDesc = "DestStateName", VarType = "Char",
VarDesc = "DestWac", VarType = "Num",
VarDesc = "Break", VarType = "Char",
VarDesc = "CouponType", VarType = "Char",
VarDesc = "TkCarrier", VarType = "Char",
VarDesc = "OpCarrier", VarType = "Char",
VarDesc = "RPCarrier", VarType = "Char",
VarDesc = "Passengers", VarType = "Num",
VarDesc = "FareClass", VarType = "Char",
VarDesc = "Distance", VarType = "Num",
VarDesc = "DistanceGroup", VarType = "Num",
VarDesc = "Gateway", VarType = "Num",
VarDesc = "ItinGeoType", VarType = "Num",
VarDesc = "CouponGeoType", VarType = "Num"
),
encode = "form",
query = list(
Table_ID = "289",
Has_Group = "0",
Is_Zipped = "0"
)
) -> res
The code used to extract the cURL was this:
library(curlconverter)
straighten() %>% make_req() -> tmp
The new saved res, had binary data which I could easily use to download the zip file generated by the POST.
res
## Response [https://transtats.bts.gov/ftproot/TranStatsData/351117019_T_DB1B_COUPON.zip]
## Date: 2018-10-14 02:18
## Status: 200
## Content-Type: application/x-zip-compressed
## Size: 14.6 MB
## <BINARY BODY>
However, when I do the same now it's a mess and I can't get anything from it.
httr::VERB(
verb = "GET", url = "https://www.transtats.bts.gov/DL_SelectFields.aspx",
query = list(
gnoyr_VQ = "FIL",
QO_fu146_anzr = "Nv4 Pn44vr45' -X 'POST' -H 'Accept: text/html,application/xhtml xml,application/xml;q=0.9,*/*;q=0.8' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Origin: https://www.transtats.bts.gov' -H 'Cookie: _ga=GA1.2.279767402.1654087030; _ga_NQ5ZN114SB=GS1.1.1654601577.6.1.1654602173.0; _gat_gtag_UA_18660041_1=1; _gid=GA1.2.975702239.1654601578; f5avr1969959209aaaaaaaaaaaaaaaa_cspm_=DKBJFJCFEDEGJMPHAKDLLGKAOMIGFJJBLFKJJFLCOKMFKOPOIBLJNFBDAENMKNMNNBNCKLDMCJCFKOMIFAFAGDCBBEIKHALBFLPDDHNGGPNBOHCDHALHLHAFCPDCECCK; _gat=1; __utma=261918792.279767402.1654087030.1654149013.1654601578.6; __utmb=261918792.6.10.1654601578; __utmc=261918792; __utmz=261918792.1654087030.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _ga=GA1.3.279767402.1654087030; _gat_b=1; _gid=GA1.3.975702239.1654601578; has_js=1; QSI_SI_e8TSnTOxZxScWdT_intercept=true; QSI_HistorySession=https://www.transtats.bts.gov/DL_SelectFields.aspx?gnoyr_VQ=GEE&QO_fu146_anzr=Nv4 Pn44vr45~1653914765211|https://www.transtats.bts.gov/DL_SelectFields.aspx?gnoyr_VQ=GEE&QO_fu146_anzr=Nv4 Pn44vr45~1653918802064|https://www.transtats.bts.gov/DL_SelectFields.aspx?gnoyr_VQ=FIL&QO_fu146_anzr=Nv4 Pn44vr45~1653926488227|https://www.transtats.bts.gov/DL_SelectFields.aspx?gnoyr_VQ=FIL&QO_fu146_anzr=Nv4 Pn44vr45~1653933301130|https://www.transtats.bts.gov/DL_SelectFields.aspx?gnoyr_VQ=FJE&QO_fu146_anzr=Nv4Pn44vr45~1653943359984|https://www.transtats.bts.gov/DL_SelectFields.aspx?gnoyr_VQ=FIL&QO_fu146_anzr=Nv4 Pn44vr45~1654090368888|https://www.transtats.bts.gov/DL_SelectFields.aspx?gnoyr_VQ=FIL&QO_fu146_anzr=Nv4 Pn44vr45~1654092812643|https://www.transtats.bts.gov/DL_SelectFields.aspx?gnoyr_VQ=FLM&QO_fu146_anzr=b4vtv0 n0q Qr56v0n6v10 f748rB~1654094623107|https://www.transtats.bts.gov/DL_SelectFields.aspx?gnoyr_VQ=FIL&QO_fu146_anzr=Nv4%Pn44vr45~1654102786829|https://www.transtats.bts.gov/DL_SelectFields.aspx?gnoyr_VQ=FIL&QO_fu146_anzr=Nv4 Pn44vr45~1654601914519; f5avraaaaaaaaaaaaaaaa_session_=PBGOPMEEHCNFKKAPFNCACAKCCBFOPNPHICHCBEFGHCMKMLBFFNNFBOCOFLEKPFCNHNLDMANNOIAOFKNKGMJADPMKGDFMDGGEFHKCCGNOLACEFOLDEKIKCIOLAIIANEKM; ASPSESSIONIDQCQCSDSA=JPCOCLGDHAIJOKGLBBJNAKCN; _ga_TS9639848Y=GS1.1.1654095826.2.0.1654095826.0; ASPSESSIONIDQCSATBTA=PFAPIPGDONFFBLDGBBMOOAJO; ASP.NET_SessionId=wr043omrzfn1emifxmj1wadr; ASPSESSIONIDQCSASBTB=BMMLLHNBPOKMOHFPOHEOJOOB' -H 'Content-Length: 8825' -H 'Accept-Language: en-us' -H 'Host: www.transtats.bts.gov' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Safari/605.1.15' -H 'Referer: https://www.transtats.bts.gov/DL_SelectFields.aspx?gnoyr_VQ=FIL",
QO_fu146_anzr = "Nv4 Pn44vr45' -H 'Accept-Encoding: gzip, deflate, br' -H 'Connection: keep-alive' --data '__EVENTTARGET=chkAllVars",
`__EVENTARGUMENT` = "",
`__LASTFOCUS` = "", `__VIEWSTATE` = "s5lt5rBqZX9v/JLyUuO6kxf qIUW68llMVM3sydtYijhto3Fidu3O/WGJ1VjJcRr1BeRbJcXFN89qYPysApyatjfXjGvzeBGgpTbMcIpSxztQwdL/cWbPv4txnZ dOJQer1kYzuvfBl08iyWWck97yWXFANUe7FQ8cv7aw8Wm/8E 7GR9Rcv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Lak1aowvcGEJmVI3Z01Kg9OJwFyaLamVWZx9t4GfCBPkXK3Ep8sW8uvARqxolVuuK1fNeJwFAD05a8VqKnD30Fe7Bn5oI0CLwiIsB8Ou7W5yExnBHmBTyeNzMP/0qeo6PTCbH88UrD9DPPXWMFXWvR7cXpAeSRsAHjtbVPwFq3bNxEunExbP54adWifEPsQs7zmBf8xQV2W1F2Rd1wmJ7DvfhwyVyITcUGig4/WcBHkEFHtCHuQvNzWTePbP5osJgeM pTDU=",
txtSearch = "", cboGeography = "All",
cboYear = "2022", cboPeriod = "All",
chkAllVars = "on", UNIQUE_CARRIER = "on",
UNIQUE_CARRIER_NAME = "on",
ORIGIN_AIRPORT_ID = "on",
ORIGIN = "on", DEST_AIRPORT_ID = "on",
DEST = "on", MONTH = "on"
)
) -> res
In fact, when trying to run that res, it won't get any zip file anymore, and I suspect that it's due to this __EVENTVALIDATION
and/or the __VIEWSTATE
. I've been reading a lot about it, but I still can't figure out how to get around it so I can download the zip file from R.
CodePudding user response:
Fixed by doing this. I'm not sure why is it that I need a second request. One with GET and one with getURL. It seems that not having the GET request doesn't create a cookie for some reason. However, it works. But if someone could explain why, this would be amazing.
curl <- getCurlHandle()
curlSetOpt(cookiejar = paste(tempdir(), "/", "cookies.txt", sep = ""),
followlocation = TRUE, autoreferer = TRUE, curl = curl)
message("Connecting to T100")
html <- getURL("https://www.transtats.bts.gov/DL_SelectFields.aspx?gnoyr_VQ=FIM&QO_fu146_anzr=Nv4%Pn44vr45", curl = curl)
temp <- GET("https://www.transtats.bts.gov/DL_SelectFields.aspx?gnoyr_VQ=FIM&QO_fu146_anzr=Nv4%Pn44vr45", curl = curl)
message("Done")
viewstate <- as.character(sub('.*id="__VIEWSTATE" value="([0-9a-zA-Z /=]*).*', '\\1', html))
viewstategenerator <- as.character(sub('.*id="__VIEWSTATEGENERATOR" value="([0-9a-zA-Z /=]*).*', '\\1', html))
eventvalidation <- as.character(sub('.*id="__EVENTVALIDATION" value="([0-9a-zA-Z /=]*).*', '\\1', html))
message("Downloading file")
httr::POST(
config = progress(),
url = "https://www.transtats.bts.gov/DL_SelectFields.aspx?gnoyr_VQ=FIM&QO_fu146_anzr=Nv4 Pn44vr45",
httr::add_headers(
Referer = "https://www.transtats.bts.gov/DL_SelectFields.aspx?gnoyr_VQ=FIM&QO_fu146_anzr=Nv4%Pn44vr45"
),
body = list(
'__EVENTARGUMENT' = "",
'__LASTFOCUS' = "",
"__VIEWSTATE" = viewstate,
'__VIEWSTATEGENERATOR' = viewstategenerator,
'__EVENTVALIDATION' = eventvalidation,
txtSearch = "",
btnDownload = "Download",
cboGeography = "All",
cboYear = y,
cboPeriod = "All",
chkAllVars = "on",
UNIQUE_CARRIER = "on",
UNIQUE_CARRIER_NAME = "on",
ORIGIN_AIRPORT_ID = "on",
ORIGIN = "on",
DEST_AIRPORT_ID = "on",
DEST = "on",
MONTH = "on"
),
encode = "form", query = list(
gnoyr_VQ = "FIM",
QO_fu146_anzr = "Nv4 Pn44vr45"
),
postData = list(
text = paste("__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=", viewstate,
"&__VIEWSTATEGENERATOR=", viewstategenerator,
"&__EVENTVALIDATION=", eventvalidation,
"&txtSearch=&cboGeography=All&cboYear=",y,"&cboPeriod=All&btnDownload=Download&UNIQUE_CARRIER=on&UNIQUE_CARRIER_NAME=on&ORIGIN_AIRPORT_ID=on&ORIGIN=on&DEST_AIRPORT_ID=on&DEST=on&MONTH=on",
sep = "")
)
)