Я пытаюсь создать веб-сайт для этого веб-сайта .
Я применяю тот же код, который всегда использую для страниц веб-страниц:
url_dv1 <- "https://ec.europa.eu/commission/presscorner/detail/en/qanda_20_171?fbclid=IwAR2GqXLmkKRkWPoy3-QDwH9DzJiexFJ4Sp2ZoWGbfmOR1Yv8POdlLukLRaU" url_dv1 <- paste(html_text(html_nodes(read_html(url_dv1), "#inline-nav-1 .ecl-paragraph")), collapse = "")
Для этого веб-сайта, думал, что код, кажется, не работает. На самом деле я получаю Ошибка в UseMethod ("read_ xml"): нет применимого метода для read_ xml, примененного к объекту класса "c ('xml_document', 'xml_node')" .
Почему это так? Как я могу это исправить?
Большое спасибо!
Проблема в том, что веб-страница отображается динамически. Вы можете преодолеть это с помощью фантома js (можно скачать здесь https://phantomjs.org/download.html). Вам также понадобится собственный скрипт javascript (см. Ниже). Ниже код R работает для меня.
library(tidyverse) library(rvest) dir_js <- "path/to/a/directory" # JS code needs to be inserted here, the name of the file needs to be javascript.js url <- "https://ec.europa.eu/commission/presscorner/detail/en/qanda_20_171?fbclid=IwAR2GqXLmkKRkWPoy3-QDwH9DzJiexFJ4Sp2ZoWGbfmOR1Yv8POdlLukLRaU" system2("path/to/where/you/have/phantomjs.exe", # directory to phantomJS args = c(file.path(dir_js, "javascript.js"), url)) read_html("myhtml.html") %>% html_nodes("#inline-nav-1 .ecl-paragraph") %>% html_text() # this is the javascript code to be saved in javascript directory as javascript.js // create a webpage object var page = require('webpage').create(), system = require('system') // the url for each country provided as an argument country= system.args[1]; // include the File System module for writing to files var fs = require('fs'); // specify source and path to output file // we'll just overwirte iteratively to a page in the same directory var path = 'myhtml.html'