【问题标题】:How do I resolve "xml missing" with Rvest?如何使用 Rvest 解决“xml 缺失”问题?
【发布时间】:2020-11-20 16:04:23
【问题描述】:

这是我试图获取所需信息的网页: https://www.immobiliare.it/ricerca-mappa/Torino,TO/#/linkZona_/latitudine_45.04463/longitudine_7.68199/idContratto_1/idCategoria_23/zoom_16/pag_1

这是与我感兴趣的节点关联的 XPath:

//*[@id="box-listing"]/div[1]

使用时

out %>%html_node(xpath = '//*[@id="box-listing"]/div[1]')

出现以下错误

{xml_missing}
<NA>

【问题讨论】:

  • 我不确定html_node,但许多抓取工具不支持https。如果您只有一页,请尝试从浏览器中保存它,然后阅读您的本地副本。
  • 谢谢,经过一番研究,我相信这是由于动态更新页面所致。我正在寻找 Selenium 包并尝试弄清楚如何使用它

标签: r rvest rselenium


【解决方案1】:

为了解决您的问题,我建议您使用Rselinium

我们有两大类网站。静态网站和动态网站。 第一个在代码中包含我们需要的信息(例如 Wikipidia 网页),而第二个在代码中实际上没有信息,但每次我们需要它时它都会通过 Javascript 代码(例如例如旅行顾问)。 感谢Rselenium 库,我们能够从动态网站上抓取信息。 什么是硒? RSelenium 是一个 R 库,但我们可以在 PythonJava 等其他类型的代码中找到它,它能够模拟人类行为。 Selenium 的主要用途是测试应用程序的自动化,但并非如此。

Selenium 是一个非常大的世界 (here to deep)。

关于 Rselenium,我建议您查看以下链接:

GitHub repository

Presentation

下面是一个关于您的问题使用 Rselenium 的小例子:

    library(RSelenium)
    
    #We start the RSelenium environment
    driver <- rsDriver(browser=c("firefox"),port = 4445L)
    remote_driver <- driver[["client"]]  
    
    #We send the url to the firefox browser
    remote_driver$navigate("https://www.immobiliare.it/ricerca-mappa/Torino,TO/#/linkZona_/latitudine_45.04462/longitudine_7.68199/idContratto_1/idCategoria_23/zoom_16/pag_1")
    
    Below some example of the Rselenium powerful
    
    #We get the text
    text_1<-remote_driver$findElement(using = "css selector", '#box-listing > div:nth-child(1) > div:nth-child(1)')$getElementText()
    print(text_1)
    [[1]]
    [1] "PREMIUM\nImmobile\n€ 150.000\n60 m² • 2 locali"

    #We click the element
    remote_driver$findElement(using = "css selector", '#box-listing > div:nth-child(1) > div:nth-child(1)')$clickElement()

【讨论】:

    猜你喜欢
    • 2022-10-24
    • 1970-01-01
    • 2012-03-30
    • 2021-06-20
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    相关资源
    最近更新 更多