2017-02-22 40 views
1

我试图抓取Crossfit Games打开排行榜。我有一个在过去几年工作过的版本,但网站改变了,我似乎无法更新我的代码,以使其与新网站一起工作。抓取网络数据,为rvest找到合适的选择器(我认为)

我的问题是我似乎无法得到正确的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经验丰富,但这是我所做过的唯一真正的网络抓取项目,所以我可能错过了一些非常基本的东西。

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

+2

“你可以不使用任何数据挖掘,机器人,刮 或类似的数据收集或提取方法来获取网站内容[...]” – GGamba

回答

3

看起来这个网页的内容是用一些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/

+0

感谢。我怀疑我需要像RSelenium这样的东西。我会尝试一下。 – BrianDavisStats