【问题标题】:Web scraping element missing in htmlhtml中缺少Web抓取元素
【发布时间】:2020-12-27 18:27:10
【问题描述】:

我在 R 中有这个简单的抓取代码

library(rvest)

url_blue <- "https://www.cronista.com/MercadosOnline/moneda.html?id=ARSB"

blue <- read_html(url_blue) %>% 
  html_nodes(xpath="//div[@class='col-venta']//span") %>% 
  html_text()

结果是:"VENTA:" ""

第一个元素没问题。第二个要素是问题。

在网页中元素是这个并且有一个值:

这很奇怪,我在网上找不到解决方案。有什么我不明白的地方吗?

非常感谢!谢谢!

【问题讨论】:

    标签: r web-scraping tidyverse rvest httr


    【解决方案1】:

    Check this XHR log here you can find the URL of that data

    url_blue <- "https://www.cronista.com/MercadosOnline/json/getValoresCalculadora.html"
    
    
    blue <- read_html(url_blue) %>% html_text()
    

    现在,我认为您可以从中获得丰硕的成果。节日快乐!!

    【讨论】:

    • 该链接是一个 JSON 文件,您需要 jsonlite 转换为 R 对象而不是 rvest::read_html(): df &lt;- jsonlite::fromJSON("https://www.cronista.com/MercadosOnline/json/getValoresCalculadora.html")
    【解决方案2】:

    一种可能的解决方案是使用Rselenium 库。

    以下可能的解决方案:

    library(RSelenium)
    driver <- rsDriver(browser=c("firefox"), port = 4449L)
    remote_driver <- driver[["client"]]
    remote_driver$navigate('https://www.cronista.com/MercadosOnline/moneda.html?id=ARSB')
    compra<-remote_driver$findElement(using = 'css selector', value = ".col-compra")$getElementText()
    print(compra)
    [1] "COMPRA:\n154,00"
    venta<-remote_driver$findElement(using = 'css selector', value = ".col-venta")$getElementText()
    print(venta)
    [1] "VENTA:\n159,00"
    

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 2021-07-17
      • 2019-12-10
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      相关资源
      最近更新 更多