петля webscraping над узлом URL и html в R rvest - PullRequest
0 голосов
/ 28 февраля 2019

У меня есть фрейм данных pubs с двумя столбцами: url, html.node.Я хочу написать цикл, который читает каждый URL-адрес, извлекает html-содержимое и извлекает информацию, указанную в столбце html.node, и накапливает ее во фрейме данных или списке.
Все URL разные, все узлы html разные.
Пока мой код:

score <- vector()
k <- 1
for (r in 1:nrow(pubs)){
  art.url <- pubs[r, 1] # column 1 contains URL
  art.node <- pubs[r, 2] # column 2 contains html nodes as charcters

  art.contents <- read_html(art.url)
  score <- art.contents %>% html_nodes(art.node) %>% html_text()
  k<-k+1
  print(score)
}

Я ценю вашу помощь.

Ответы [ 2 ]

0 голосов
/ 28 февраля 2019

Вы также можете использовать map из пакета purrr вместо цикла:

expand.grid(c("http://quotes.toscrape.com/", "http://quotes.toscrape.com/tag/inspirational/"), # vector of urls
  c(".text",".author"), # vector of nodes
  stringsAsFactors = FALSE) %>% # assuming that the same nodes are relevant for all urls, otherwise you would have to do something like join
  as_tibble() %>%
  set_names(c("url", "node")) %>%
  mutate(out = map2(url, node, ~ read_html(.x) %>% html_nodes(.y) %>% html_text())) %>%
  unnest()
0 голосов
/ 28 февраля 2019

Прежде всего, убедитесь, что каждый сайт, который вы собираетесь очистить, позволяет очищать данные, которые вы можете взять на себя в юридическом вопросе, если нарушите некоторые правила.

(Обратите внимание, яВы использовали только http://toscrape.com/, сайт песочницы для очистки, поскольку вы не предоставили свои данные)

После этого вы можете продолжить, надеясь, что это поможет:

# first, your data I think they're similar to this
pubs <- data.frame(site = c("http://quotes.toscrape.com/",
                            "http://quotes.toscrape.com/"),
                   html.node = c(".text",".author"), stringsAsFactors = F)

Тогда требуемый цикл:

library(rvest)
# an empty list, to fill with the scraped data
empty_list <- list()

# here you are going to fill the list with the scraped data
for (i in 1:nrow(pubs)){
  art.url <- pubs[i, 1]   # choose the site as you did
  art.node <- pubs[i, 2]  # choose the node as you did      
  # scrape it!    
  empty_list[[i]] <- read_html(art.url)  %>% html_nodes(art.node) %>% html_text()

}

Теперь результатом является список, но с:

names(empty_list) <- pubs$site

Вы собираетесь добавить к каждому элементусписок с названием сайта, с результатом:

$`http://quotes.toscrape.com/`
 [1] "“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”"                
 [2] "“It is our choices, Harry, that show what we truly are, far more than our abilities.”"                                              
 [3] "“There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.”"
 [4] "“The person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.”"                           
 [5] "“Imperfection is beauty, madness is genius and it's better to be absolutely ridiculous than absolutely boring.”"                    
 [6] "“Try not to become a man of success. Rather become a man of value.”"                                                                
 [7] "“It is better to be hated for what you are than to be loved for what you are not.”"                                                 
 [8] "“I have not failed. I've just found 10,000 ways that won't work.”"                                                                  
 [9] "“A woman is like a tea bag; you never know how strong it is until it's in hot water.”"                                              
[10] "“A day without sunshine is like, you know, night.”"                                                                                 

$`http://quotes.toscrape.com/`
 [1] "Albert Einstein"   "J.K. Rowling"      "Albert Einstein"   "Jane Austen"       "Marilyn Monroe"    "Albert Einstein"   "André Gide"       
 [8] "Thomas A. Edison"  "Eleanor Roosevelt" "Steve Martin"   

Понятно, что он должен работать с разными сайтами и разными узлами.

...