2017-02-22 34 views
-1

我一直在使用R和httrplyr庫從API中提取數據。它非常直截了當,並用下面的代碼工作得很好:從XHR請求頭中提取令牌R

library(httr) 
library(plyr) 

headers <- c("Accept" = "application/json, text/javascript", 
     "Accept-Encoding" = "gzip, deflate, sdch", 
     "Connection" = "keep-alive", 
     "Referer" = "http://www.afl.com.au/stat", 
     "Host" = "www.afl.com.au", 
     "User-Agent" = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36", 
     "X-Requested-With"= "XMLHttpRequest", 
     "X-media-mis-token" = "f31fcfedacc75b1f1b07d5a08887f078") 

query <- GET("http://www.afl.com.au/api/cfs/afl/season?seasonId=CD_S2016014", add_headers(headers)) 

stats <- httr::content(query) 

我的問題是與問候在頭所需的請求令牌(即X-媒體誤標記)。這很容易通過檢查Chrome或Firefox中的XHR元素來手動獲取,但令牌每24小時更新一次,使手動抽取變得非常痛苦。

是否有可能使用R自動查詢網頁並提取此令牌?

+0

你能檢查答案並進行投票和檢查嗎? –

回答

0

您可以獲得X-media-mis-token標記,但帶有免責聲明。 ;)

library(httr) 
token_url <- 'http://www.afl.com.au/api/cfs/afl/WMCTok' 
token <- POST(token_url, encode="json") 
content(token)$token 
#[1] "f31fcfedacc75b1f1b07d5a08887f078" 
content(token)$disclaimer 
#[1] "All content and material contained within this site is protected by copyright owned by or licensed to Telstra. Unauthorised reproduction, publishing, transmission, distribution, copying or other use is prohibited. 
+0

真棒Karthik,這完美的作品。謝謝! – Safarr

+0

@Safarr請投票並檢查答案。謝謝。 –

+0

答案是正確的Karthik(我認爲這從我以前的評論是清楚的?)。我已經投了票,但它不算,因爲我沒有足夠的聲望點。 – Safarr