我想从 hockey-reference.com 中抓取数据,特别是从以下链接中抓取数据:
https://www.hockey-reference.com/leagues/NHL_1991.html https://www.hockey-reference.com/leagues/NHL_1991.html
我想要第四个表,称为“团队统计”,我还想减去第一行和最后一行(但这可以是另一次)。
最初,我希望抓取 1991 年的链接,但我希望最终抓取 1991 年到 2017 年的每个链接。
library(tidyverse)
library(rvest)
stat_urls <- "https://www.hockey-reference.com/leagues/NHL_1991.html"
现在,为了简单起见,我只有 1991 年的链接。在使用实际网页的“检查”源进行相当彻底的搜索之后,我似乎无法找到正确的 css 选择,即使我已经尝试了多种不同的选择。我尝试过以下 css 选择:
table#stats.sortable.stats_table.now.sortable
#stats
#all_stats
#all_stats > div.table_outer_container
#stats
#stats > tbody
#div_stats (and all sorts of combos with this one)
当在以下代码中使用时,这些都不起作用:
team_stats <- stat_urls %>%
read_html() %>%
html_nodes("#stats") %>%
html_table(header = T)
所有使用“xpath=”的尝试也都失败了。对此的任何帮助都将是绝对惊人的,Go Preds!
您可以尝试使用 RSelenium。在这里看到了类似的答案:使用 R 的网页抓取篮球参考 https://stackoverflow.com/questions/48778493/web-scraping-basketball-reference-using-r.
library(rvest)
library(RSelenium)
startServer()
remDr<-remoteDriver(browserName = "chrome")
remDr$open()
remDr$navigate("https://www.hockey-reference.com/leagues/NHL_1991.html")
page <- read_html(remDr$getPageSource()[[1]])
table <- html_table(page, fill = TRUE)
table[[28]]
虽然安装 selenium 很痛苦,我也会尝试提供帮助,但我不久前安装了它,所以不太记得了。祝你好运
来自发布原始问题的人:
上面的答案有效,但我必须通过 Homebrew:
https://brew.sh/
然后我不得不使用这里的以下代码:
# download selenium jar
curl -L0 https://selenium-release.storage.googleapis.com/3.9/selenium-
server-standalone-3.9.1.jar -o selenium-server-standalone.jar
# install chromedriver
brew install chromedriver
# start chrome driver
brew services start chromedriver
#==> Successfully started `chromedriver`
(label:homebrew.mxcl.chromedriver)
# start selenium server
java -jar selenium-server-standalone.jar
#14:38:20.684 INFO - Selenium build info: version: '3.9.1', revision:
'63f7b50'
#14:38:20.685 INFO - Launching a standalone Selenium Server on port
4444
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)