Отправьте URL-адреса из столбца фрейма данных с помощью rvest

У меня есть фрейм данных под названием dogs, который выглядит так:

url 
https://en.wikipedia.org/wiki/Dog
https://en.wikipedia.org/wiki/Dingo
https://en.wikipedia.org/wiki/Canis_lupus_dingo

Я хотел бы отправить все URL-адреса в rvest, но не знаю, как это сделать.

я пробовал это

dogstext <-html(dogs$url) %>%
    html_nodes("p:nth-child(4)") %>%
    html_text() 

но я получил эту ошибку

Error in UseMethod("parse") : 
  no applicable method for 'parse' applied to an object of class "factor"

person Blas    schedule 02.06.2015    source источник


Ответы (2)


Как говорит ошибка, вам нужно преобразовать столбец фактора в символ перед разбором:

dogs$url<-as.character(dogs$url)

а затем ваш код следует за этим.

Обновлять:

dog<-data.frame(url=c("https://en.wikipedia.org/wiki/Dog","https://en.wikipedia.org/wiki/Dingo","https://en.wikipedia.org/wiki/Canis_lupus_dingo"))
> str(dog)
'data.frame':   3 obs. of  1 variable:
 $ url: Factor w/ 3 levels "https://en.wikipedia.org/wiki/Canis_lupus_dingo",..: 3 2 1
> lapply(as.character(dog$url),function(i)dogstext <-html(i) %>%
          html_nodes("p:nth-child(4)") %>%
            html_text() )
[[1]]
[1] "The domestic dog (Canis lupus familiaris or Canis familiaris) is a domesticated canid which has been selectively bred for millennia for various behaviors, sensory capabilities, and physical attributes.[2] The global dog population is estimated to between 700 million[3] to over one billion, thus making the dog the most abundant member of order Carnivora.[4]"

[[2]]
[1] "The dingo's habitat ranges from deserts to grasslands and the edges of forests. Dingoes will normally make their dens in deserted rabbit holes and hollow logs close to an essential supply of water."

[[3]]
character(0)
person user227710    schedule 02.06.2015
comment
Спасибо, я попытался получить эту ошибку. Ошибка: длина (url) == 1 не является TRUE Кроме того: Предупреждающее сообщение: In if (grepl (^http, x)) { : условие имеет длину › 1 и только первый элемент будет использоваться - person Blas; 03.06.2015

Вы также можете полностью использовать идиому конвейера (%>%) и (при необходимости) добавить столбец с извлеченным текстом обратно в исходный фрейм данных или сохранить его как вектор. Приведенный ниже метод также делает код более читабельным.

library(rvest)
library(dplyr)

dog <- data.frame(url=c("https://en.wikipedia.org/wiki/Dog",
                        "https://en.wikipedia.org/wiki/Dingo",
                        "https://en.wikipedia.org/wiki/Canis_lupus_dingo"))

# this keeps the code clean and readable and testable

extract <- function(x, css) {

  # this catches retrieval errors

  pg <- try(html(x), silent=TRUE)

  # if any retrieval error, return NA

  if (inherits(pg, "try-error")) { return(NA) }

  pg %>% 
    html_nodes(css) %>%
    html_text -> element

  # if there is no matching element the resule will be a 0 length list
  # which will prevent sapply from simplifying it, so test for that here

  element <- ifelse(length(element) == 0, NA, element)

  element

}

# add as a column to the original data frame

dog %>% mutate(text=sapply(as.character(url), extract, "p:nth-child(4)")) -> dog

glimpse(dog)

## Observations: 3
## Variables:
## $ url  (fctr) https://en.wikipedia.org/wiki/Dog, https://en.wikipedia....
## $ text (chr) "The domestic dog (Canis lupus familiaris or Canis famili...

# or just get it out as a separate vector

dog$url %>%
  as.character %>%
  sapply(extract, "p:nth-child(4)")

##                                                                                                                                                                                                                                                                                                                                        https://en.wikipedia.org/wiki/Dog 
## "The domestic dog (Canis lupus familiaris or Canis familiaris) is a domesticated canid which has been selectively bred for millennia for various behaviors, sensory capabilities, and physical attributes.[2] The global dog population is estimated to between 700 million[3] to over one billion, thus making the dog the most abundant member of order Carnivora.[4]" 
##                                                                                                                                                                                                                                                                                                                                      https://en.wikipedia.org/wiki/Dingo 
##                                                                                                                                                                  "The dingo's habitat ranges from deserts to grasslands and the edges of forests. Dingoes will normally make their dens in deserted rabbit holes and hollow logs close to an essential supply of water." 
##                                                                                                                                                                                                                                                                                                                          https://en.wikipedia.org/wiki/Canis_lupus_dingo 
##                                                                                                                                                                                                                                                                                                                                                                       NA
person hrbrmstr    schedule 03.06.2015
comment
Он работает очень хорошо, пока не найдет 404, а затем остановит процесс. Ошибка в parse.response(r, parser, encoding = encoding): ошибка клиента: (404) Not Found - person Blas; 05.06.2015
comment
Все, что вам нужно было сделать, это использовать try. Обновлено. - person hrbrmstr; 05.06.2015