我怎样才能抓取这些数据?

时间:2015-04-08 15:15:24

标签: r web-scraping rvest

我想从此页面中删除统计信息:

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

具体来说,我想抓住斯图亚特爆头下的表格中的数据。它由Stuart Appleby主演 - 2015年STATS PGA巡回演唱会&#34;

我尝试使用rvest,与选择器小工具(http://selectorgadget.com/)结合使用。

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

&#39;宜&#39;把我拿到桌子上,例如,顶部的那行表示&#34; Recap - Rank - Additional Stats&#34;

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

&#39;宜&#39;让我把这个&#34;回顾 - 排名 - 添加&#39; l统计&#34;线。

也没有。

在网络抓取方面,我是一个完整的新手。当我点击查看来源&#39;对于该网页,表中包含的数据不存在。

在源代码中,我认为表应该开始,这是一段代码:

<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等效来获取以这种方式存储的数据?

感谢。

2 个答案:

答案 0 :(得分: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")

答案 1 :(得分:1)

检查一下。

GitHub上的开源项目抓取PGA数据:https://github.com/zachwill/golf/blob/master/pga.py

相关问题