【问题标题】:How can I scrape this data?我怎样才能抓取这些数据?
【发布时间】:2015-06-13 15:32:18
【问题描述】:

我想从这个页面抓取统计数据:

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

具体来说,我想获取 Stuart 头像下方的表格中的数据。标题为“Stuart Appleby - 2015 STATS PGA TOUR”

我尝试将 rvest 与 Selector Gadget (http://selectorgadget.com/) 结合使用。

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

“应该”让我得到表格,例如,顶部没有显示“Recap -- Rank -- Additional Stats”的行

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

“应该”给我一张带有“Recap -- Rank -- Add'l Stats”行的表格。

也不行。

在网络抓取方面,我完全是新手。当我点击该网页的“查看源代码”时,表格中包含的数据不存在。

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

<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 
.
.
.

因此,该表似乎存储在 html() 函数无法访问的某处(Json?Jquery?Javascript?这些术语是否适用于此处?)。有没有办法使用rvest 来获取这些数据?是否有 rvest 等效项用于抓取以这种方式存储的数据?

谢谢。

【问题讨论】:

  • 任何帮助您的人都违反了他们的 ToC - You shall not use or permit or facilitate others to use PGATOUR.com by automated electronic processes, robots, spiders, scrapers, webcrawlers, or other computer programs that monitor, copy or download data or other content found on or accessed through PGATOUR.com, including without limitation real time scoring, video, audio, statistics, polling, or data content, whether current or archival.

标签: r web-scraping rvest


【解决方案1】:

我可能会使用页面发出的 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")

【讨论】:

  • 谢谢!问题:您是如何发现http://www.pgatour.com/data/players/20098/2014stat.json 的网址的?
  • 在 chrome 中...右键单击,检查元素。然后单击网络选项卡。然后使用网页上的下拉菜单更改年份,并查看网络标签以获取新条目。
  • 谢谢!在我右键单击 > 检查 > 网络后,我实际上已经有一年无法单击下拉框了。我可以单击播放器的下拉列表,但不能单击年份。此外,当我为播放器执行下拉菜单时,网络选项卡中似乎有大量新条目。你有什么方法可以筛选出重要的吗?
  • GET 或 POST 条目是您应该键入的条目。您有时必须检查标题并将其复制到 POST 中,但 GET 通常很容易,就像上面一样...
【解决方案2】:

看看这个。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-09
    • 2013-11-09
    • 2016-08-28
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    相关资源
    最近更新 更多