【发布时间】: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®ion=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