Вам просто нужно изменить page_source
на page_source[[1]]
и быть немного более осторожным с именами переменных (например, индексаторами, векторами) и вызовами.Я бы также порекомендовал вам распечатать какое-то сообщение при использовании подобных циклов.Кроме того, если вы удалите remDr$close()
, вы можете избежать потери связи.Наконец, вы можете сохранить результаты в переменной вне цикла:
scrapped = list()
i <- 1
while(i <= 4){
url <- webpages$url[i]
print( paste("Accessing to:", url) )
remDr$navigate(url)
page_source <- remDr$getPageSource()
URL <- read_html(page_source[[1]])
results <- URL %>% html_nodes("head")
records <- vector("list", length = length(results))
for (ii in seq_along(records)) {
title <- xml_contents(results[ii] %>% html_nodes("title"))[1] %>%
html_text(trim = TRUE)
desc <- results[ii] %>%
html_nodes("meta[name=description]") %>%
html_attr("content")
keywords <- results[ii] %>%
html_nodes("meta[name=keywords]") %>%
html_attr("content")
}
#remDr$close()
scrapped[[i]] = data.frame(name = webpages[i,'name'],
url = webpages[i,'url'],
title = ifelse(length(title) > 0, title, NA),
description = ifelse(length(desc) > 0, desc, NA),
keywords = ifelse(length(keywords) > 0, keywords, NA))
i = i + 1
}
Вывод
do.call('rbind', scrapped)
# name url title
#1 amazon http://www.amazon.com Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more
#2 apple http://www.apple.com Apple
#3 usps http://www.usps.com Welcome | USPS
#4 yahoo http://www.yahoo.com Yahoo
description
#1 Online shopping from the earth's biggest selection of books, magazines, music, DVDs, videos, electronics, computers, software, apparel & accessories, shoes, jewelry, tools & hardware, housewares, furniture, sporting goods, beauty & personal care, broadband & dsl, gourmet food & just about anything else.
#2 <NA>
#3 Welcome to USPS.com. Find information on our most convenient and affordable shipping and mailing services. Use our quick tools to find locations, calculate prices, look up a ZIP Code, and get Track & Confirm info.
#4 Las noticias, el correo electrónico y las búsquedas son tan solo el comienzo. Descubre algo nuevo todos los días en Yahoo.
#keywords
#1 Amazon, Amazon.com, Books, Online Shopping, Book Store, Magazine, Subscription, Music, CDs, DVDs, Videos, Electronics, Video Games, Computers, Cell Phones, Toys, Games, Apparel, Accessories, Shoes, Jewelry, Watches, Office Products, Sports & Outdoors, Sporting Goods, Baby Products, Health, Personal Care, Beauty, Home, Garden, Bed & Bath, Furniture, Tools, Hardware, Vacuums, Outdoor Living, Automotive Parts, Pet Supplies, Broadband, DSL
#2 <NA>
#3 Quick Tools, Shipping Services, Mailing Services, Village Post Office, Ship Online, Flat Rate, Postal Store, Ship a Package, Send Mail, Manage Your Mail, Business Solutions, Find Locations, Calculate a Price, Look Up a ZIP Code, Track Packages, Print a Label, Stamps
#4 yahoo, yahoo inicio, yahoo página de inicio, yahoo búsqueda, correo yahoo, yahoo messenger, yahoo juegos, noticias, finanzas, deportes, entretenimiento