2015-04-08 52 views
1

我想從這個網頁刮統計:如何刮取這些數據?

url <- "http://www.pgatour.com/players/player.20098.stuart-appleby.html/statistics" 

具體來說,我要搶在這司徒爆頭下面的表中的數據。它是「Stuart Appleby - 2015 STATS PGA TOUR」的頭條新聞。

我嘗試使用rvest,與Selector Gadget(http://selectorgadget.com/)組合使用。

url_html <- url %>% html() 
url_html %>% 
     html_nodes(xpath = '//*[(@id = "playerStats")]//td') 

「應該」讓我的表沒有,例如,在頂部有一行寫着「重溫 - 等級 - 更多統計」

url_html <- url %>% html() 
url_html %>% 
    html_nodes(xpath = '//*[(@id = "playerStats")] | //th//*[(@id = "playerStats")]//td') 

「應該」讓我用表即「重述 - 排名 - 添加統計」行。

也沒有。

Obvs我是一個完整的newb當涉及到網絡抓取。當我點擊該網頁的「查看源代碼」時,表中包含的數據不存在。

在源代碼中,在那裏我認爲表應該開始,是這段代碼:

<script id="playerStatsTourTemplate" type="text/x-jquery-tmpl"> 
    {{each(t, tour) tours}} 
     {{if pgatour.players.shouldProcessTour(tour.tourCodeLC)}} 
     <div class="statistics-head"> 
      <h2 class="title">Stuart&nbsp;Appleby - <b>${year} STATS 
. 
. 
. 

所以,它出現在表中存儲的地方(JSON jQuery的使用Javascript是誰???這些條款適用於此?),這是html()函數無法訪問的。無論如何可以使用rvest來獲取這些數據嗎?是否有一個rvest等同於抓取以這種方式存儲的數據?

謝謝。

+1

任何人誰可以幫助你違反了他們的ToC的 - '你不得使用或允許或協助他人通過自動化的電子流程,機器人使用PGATOUR.com,監視,複製或下載在PGATOUR.com上找到或通過PGATOUR.com訪問的數據或其他內容,包括但不限於實時評分,視頻,音頻,統計數據,輪詢或數據內容(無論是當前的或檔案.' – hrbrmstr

回答

2

我可能會使用GET請求,該頁面正在爲得到解析,從他們的API和工作的原始資料...

content(a)給你一個列表表示...基本輸出fromJSON()

as(a, "character")給你生JSON

library("httr") 
a <- GET("http://www.pgatour.com/data/players/20098/2014stat.json") 
content(a) 
as(a, "character") 
+0

謝謝!問:你是如何發現url http://www.pgatour.com/data/players/20098/2014stat.json? – hossibley

+0

在chrome中...右鍵單擊,檢查元素。然後點擊網絡標籤。然後使用網頁上的下拉菜單更改年份並查看新條目的網絡選項卡。 – cory

+0

謝謝!在我點擊右鍵>檢查>網絡後,我實際上一直無法點擊下拉框。我可以點擊播放器的下拉菜單,但不是一年。另外,當我爲播放器執行下拉菜單時,網絡標籤中會出現大量新條目。你有過篩選重要的方法嗎? – hossibley