我对R相当熟悉,但是对于Web抓取具有0经验。我环顾四周,似乎无法弄清为什么我的网页抓取“失败”。这是我的代码,包括我要抓取的网址(具体来说是ngs-data-table):
library(rvest)
webpage <- read_html("https://nextgenstats.nfl.com/stats/rushing/2020/REG/1#yards")
tbls <- html_nodes(webpage, xpath = '/html/body/div[2]/div[3]/main/div/div/div[3]')
#also attempted using this Xpath '//*[@id="stats-rushing-view"]/div[3]' but neither worked
tbls
我的代码没有收到任何错误,但是我收到:
{xml_nodeset (0)}
我知道这不是很多代码,我也尝试了多个不同的xpath。我知道我最终将需要更多代码来更具体地进行爬网,但是我认为即使上面的代码也至少会为我指明正确的方向?任何帮助,将不胜感激。谢谢!
数据存储为JSON。这是下载和处理该文件的方法。
library(httr)
#URL for week 6 data
url <- "https://nextgenstats.nfl.com/api/statboard/rushing?season=2020&seasonType=REG&week=6"
#create a user agent
ua <- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
#download the information
content <-httr::GET(url, verbose() , user_agent(ua), add_headers(Referer = "https://nextgenstats.nfl.com/stats/rushing/2020/REG/1"))
answer <-jsonlite::fromJSON(content(content, as = "text") ,flatten = TRUE)
answer$stats
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句