R rvest: could not find function “xpath_element”

╄→尐↘猪︶ㄣ 提交于 2019-12-01 04:40:59

I understand this issue is rather old but I wanted to post a comment for those who may have similar issues.

I stumbled upon this same error and was unable to find much help. So, I thought instead of targeting CSS I would try to target the xpath instead. I do not know what the best practice is.

My original functions worked fine on Ubuntu 16, R 3.4.0. However, they failed on Debian 8 R 3.3.3 and R 3.4.0.

When I modified my code to target xpaths instead of css they began working as expected. For example, changing this...

contents <- link %>% 
    xml2::read_html() %>%
    rvest::html_nodes(css = "pre") %>%
    rvest::html_text()

to this...

contents <- link %>%
    xml2::read_html() %>%
    rvest::html_nodes(xpath = "//pre") %>%
    rvest::html_text()

resolved my issue.

As pointed out by @tbrugz, the issue seems to come form package selectr.

This happens however only when the package is installed with apt-get install r-cran-selectr. Installing the package with sudo R, then install.packages works fine.

pkg <- installed.packages()
subset(as.data.frame(pkg), Package=="selectr", c("Package", "LibPath"))
      Package                                         LibPath
  selectr   selectr /home/matifou/R/x86_64-pc-linux-gnu-library/3.3
  selectr.1 selectr                         /usr/lib/R/site-library
library(selectr, lib.loc="/home/matifou/R/x86_64-pc-linux-gnu-library/3.3")
css_to_xpath(".testclass")
  [1] "descendant-or-self::*[@class and contains(concat(' ', normalize-    space(@class), ' '), ' testclass ')]"

detach("package:selectr", unload=TRUE)

library(selectr, lib.loc="/usr/lib/R/site-library")
css_to_xpath(".testclass")
  Error in do.call(method, list(parsed_selector)) : 

could not find function "xpath_class"

I solved this by updating xml2 directly to my local R library rather than relying on rvests imports.

install.packages("xml2")

had same problem. I had installed rverse directly from Jupyter notebook on the macOS 10.13.5. Reinstalling rverse directly from the r-shell fixed this for me. Steps: 1. type r in terminal and press enter. 2. install.packages('rvest')

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!