【问题标题】:No data when scraping with rvest用 rvest 抓取时没有数据
【发布时间】:2018-09-03 07:25:46
【问题描述】:

我正在尝试抓取一个网站,但它没有给我任何数据。

#Get the Data
require(tidyverse)
require(rvest)

#specify the url
url <- 'https://www.travsport.se/sresultat?kommando=tevlingsdagVisa&tevdagId=570243&loppId=0&valdManad&valdLoppnr&source=S'

#get data
url %>%
  read_html() %>% 
  html_nodes(".green div:nth-child(1)") %>% 
  html_text()
character(0)

我也尝试使用xpath = '//*[contains(concat( " ", @class, " " ), concat( " ", "green", " " ))]//div[(((count(preceding-sibling::*) + 1) = 1) and parent::*)]//a',但这给了我相同的结果,数据为 0。

我期待马的名字。即使页面上的数据是由 javascript 呈现的,我至少应该得到一些 javascript 代码吗?

我看不出我还应该在这里使用什么 CSS 选择器。

【问题讨论】:

    标签: r rvest


    【解决方案1】:

    您可以简单地使用 RSelenium 包来抓取动态页面:

    library(RSelenium)
    #specify the url
    url <- 'https://www.travsport.se/sresultat?kommando=tevlingsdagVisa&tevdagId=570243&loppId=0&valdManad&valdLoppnr&source=S'
    
    #Create the remote driver / navigator
    rsd <- rsDriver(browser = "chrome")
    remDr <- rsd$client
    
    #Go to your url
    remDr$navigate(url)
    page <- read_html(remDr$getPageSource()[[1]])
    
    #get your horses data by parsing Selenium page with Rvest as you know to do
    page %>% html_nodes(".green div:nth-child(1)") %>% html_text()
    

    希望对你有帮助

    哥达维亚诺尼

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-05
      • 2017-04-01
      • 1970-01-01
      • 2015-08-23
      相关资源
      最近更新 更多