问题
I am trying to simply replicate the example of rvest::html_nodes()
, yet encounter an error:
library(rvest)
ateam <- read_html("http://www.boxofficemojo.com/movies/?id=ateam.htm")
html_nodes(ateam, "center")
Error in do.call(method, list(parsed_selector)) : could not find function "xpath_element"
The same happens if I load packages such as httr
, xml2
, selectr
. I seem to have the latest version of these packages too...
In which packages are functions such as xpath_element
, xpath_combinedselector
located? How do I get it to work? Note that I am running on Ubuntu 16.04, so that code might work on other platforms...
回答1:
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.
回答2:
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"
回答3:
I solved this by updating xml2
directly to my local R library rather than relying on rvests
imports.
install.packages("xml2")
回答4:
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')
来源:https://stackoverflow.com/questions/40874011/r-rvest-could-not-find-function-xpath-element