【问题标题】:Scraping web-data, finding the appropriate selector for rvest (i think)抓取网络数据,为 rvest 找到合适的选择器(我认为)
【发布时间】:2017-02-22 20:47:27
【问题描述】:

我正在尝试抓取 Crossfit Games Open 排行榜。我有一个在前几年工作的版本,但网站发生了变化,我似乎无法更新我的代码以使其与新网站一起工作。

我的问题是我似乎无法获得正确的 CSS 选择器来获取运动员姓名和指向他们个人资料的链接。

我的旧代码做了类似的事情:

library(rvest)

# old site
old_url <- "https://games.crossfit.com/scores/leaderboard.php?stage=1&sort=1&page=1&division=1&region=0&numberperpage=100&competition=0&frontpage=0&expanded=0&year=16&scaled=0&full=1&showtoggles=0&hidedropdowns=1&showathleteac=1&is_mobile=1"
old_page <- read_html(old_url)

# get the athletes profile url
athlete_link <-  html_attr(html_nodes(old_page, "td.name a"), "href")
athlete_name <-  html_text(html_nodes(old_page, "td.name a"))

head(athlete_link)
# [1] "http://games.crossfit.com/athlete/124483" "http://games.crossfit.com/athlete/2725"   "http://games.crossfit.com/athlete/199938"
# [4] "http://games.crossfit.com/athlete/173837" "http://games.crossfit.com/athlete/2476"   "http://games.crossfit.com/athlete/499296"

head(athlete_name)
# [1] "Josh Bridges"    "Noah Ohlsen"     "Jacob Heppner"   "Jonne Koski"     "Luke Schafer"    "Andrew Kuechler"

# new site
new_url <- "https://games.crossfit.com/leaderboard?page=1&competition=1&year=2017&division=2&scaled=0&sort=0&fittest=1&fittest1=0&occupation=0"
new_page <- read_html(new_url)

# get the athletes profile url
# I would have thought something like this would get it.  
# It doens't seem to pull anything
html_attr(html_nodes(new_page, "td.name a.profile-link"), "href")
# character(0)

html_text(html_nodes(new_page, "td.name div.full-name"))
# character(0)

我尝试了各种其他 CSS Seclectors、SelectorGadget 和其他一些东西。我在 R 方面经验丰富,但这是我做过的唯一真正的网络抓取项目,所以我可能遗漏了一些非常基本的东西。

我应该使用哪个选择器来获取这些数据?

【问题讨论】:

  • “您不得使用任何数据挖掘、机器人、抓取或类似的数据收集或提取方法来获取网站内容 [...]”

标签: html css r web-scraping rvest


【解决方案1】:

看起来这个页面的内容是用一些 javascript 动态生成的。您可以检查页面的源代码,您会看到如下内容:

<div class="modal-body">
    <!-- dynamically generated content goes here -->
</div>

桌子应该放在哪里。在这些情况下,仅靠 rvest 是不够的。 你可以查看这篇最近的博文,它有有用的指点:https://rud.is/b/2017/02/09/diving-into-dynamic-website-content-with-splashr/

【讨论】:

  • 谢谢。我怀疑我需要像 RSelenium 这样的东西。我会试试splashr。
猜你喜欢
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2021-09-25
  • 2020-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多