【问题标题】:rvest html_nodes returns {xml_nodeset (0)}rvest html_nodes 返回 {xml_nodeset (0)}
【发布时间】:2021-04-29 13:29:40
【问题描述】:

我一直在尝试使用 rvest 和 selectorGadge 抓取 this page。我可以抓取产品描述,但是当我尝试获取图片中显示的值时:

但是,当我运行代码时:

library(dplyr)
library(rvest)

read_html("https://www.dicasanet.com.br/material-de-construcao") %>%
  html_nodes(".product-payment")

我不断得到结果“{xml_nodeset (0)}”。我注意到,与其他值(如产品名称)不同,这不是 div.a,而是 div.div。有没有另一种方法来获得这些值?我应该如何进行?提前致谢!

【问题讨论】:

    标签: html r web-scraping rvest


    【解决方案1】:

    数据是从 script 标记内的 JavaScript 对象动态加载的。您可以从响应文本中对其进行正则表达式,使用 jsonlite 解析为 json 对象,然后提取您想要的产品

    library(magrittr)
    library(rvest)
    library(stringr)
    library(jsonlite)
    
    page <- read_html('https://www.dicasanet.com.br/loja/catalogo.php?loja=790930&categoria=1')
    
    data <- page %>% 
      toString() %>% 
      stringr::str_match('dataLayer = (\\[.*\\])') %>% 
      .[2] %>% 
      jsonlite::parse_json()
    
    print(data[[1]]$listProducts)
    

    【讨论】:

      【解决方案2】:

      我无法使用 Rvest 刮掉价格,但是可以使用 RSelenium:

      library(RSelenium)
      # I use RSelenium in combination with docker.
      remDr <- remoteDriver(
        remoteServerAddr = "localhost",
        port = 4445L,
        browserName = "chrome"
      )
      remDr$open()
      remDr$navigate("https://www.dicasanet.com.br/material-de-construcao")
      
      page <- read_html(remDr$getPageSource()[[1]])
      
      page %>% html_nodes("product-price") %>% html_text()
      
      price <- remDr$findElements(using = "class","product-price")
      price <- sapply(price,function(x){x$getElementText()[[1]]})
      price
      

      输出是:

      price
       [1] ""                     ""                     ""                     ""                    
       [5] ""                     ""                     "R$ 40,50"             "R$ 40,50"            
       [9] "R$ 194,90"            "R$ 194,90"            "R$ 171,00\nR$ 122,90" "R$ 171,00\nR$ 122,90"
      [13] "R$ 393,00"            "R$ 393,00"            "R$ 357,50"            "R$ 357,50"           
      [17] "R$ 433,20"            "R$ 433,20"            "R$ 120,60"            "R$ 120,60"           
      [21] "R$ 89,50"             "R$ 89,50"             "R$ 56,20"             "R$ 56,20"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-30
        • 1970-01-01
        • 1970-01-01
        • 2019-11-23
        • 1970-01-01
        • 2018-07-15
        • 2020-11-30
        相关资源
        最近更新 更多