R rvest: could not find function "xpath_element"

I am trying to just reproduce the rvest::html_nodes() example, but 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 thing happens if I download packages like httr , xml2 , selectr . I seem to have the latest version of these packages ...

What packages have features like xpath_element , xpath_combinedselector ? How do I make it work? Please note that I work on Ubuntu 16.04, so the code may work on other platforms ...

+5
source share
3 answers

I understand that this problem is quite old, but I wanted to post a comment for those who may have similar problems.

I came across the same error and could not find much help. So, I thought that instead of targeting CSS, I will try to target xpath. I do not know what is the best practice.

My source functions worked fine on Ubuntu 16, R 3.4.0. However, they did not cope with Debian 8 R 3.3.3 and R 3.4.0.

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

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

to that...

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

solved my problem.

+2
source

As @tbrugz pointed out, the problem seems to occur in the selectr package.

This happens, however, only when the package is installed with apt-get install r-cran-selectr. Installing the package using 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"

+1
source

I solved this by updating xml2 directly in my local R library, instead of relying on importing rvests .

install.packages("xml2")

0
source

Source: https://habr.com/ru/post/1260539/


All Articles