2017-10-25 3 views
0

kratzen Ich bin neu in R und habe einige Web Scraping. Ich habe den folgenden Code geschrieben, der die ID, den Namen, die Farbe und den Preis eines bestimmten Artikels von https://uk.burberry.com/ in einen Datenrahmen setzt.Mit rvest über mehrere Webseiten in r

# load package 
library(rvest) 

# Example URL 
url <- 'https://uk.burberry.com/fringed-wool-cashmere-patchwork-cardigan-coat-p40612561' 

# Read HTML code from the website 
webpage <- read_html(url) 

# using css selectors to scrape the ID section 
id_data_html <- html_nodes(webpage, '.section') 
#converting the ID to text 
id_data <- html_text(id_data_html) 
# Remove irrelevant text 
id_data <- gsub("Item", "", id_data) 

# using css selectors to scrape the names section 
names_data_html <- html_nodes(webpage, '.type-h6') 
#converting the names to text 
names_data <- html_text(names_data_html) 
# Stripping irrelevant text 
names_data <- gsub("\n\t\t\t\t\t\t\t", "", names_data) 

# using css selectors to scrape the price section 
price_data_html <- html_nodes(webpage, '.l2') 
#converting the price to text 
price_data <- html_text(price_data_html) 
# Remove irrelevant text 
price_data <- gsub("\t", "", price_data) 
price_data <- gsub("\n", "", price_data) 

# using css selectors to scrape the colour section 
colour_data_html <- html_nodes(webpage, '#colour-picker-value') 
#converting the colour to text 
colour_data <- html_text(colour_data_html) 

# creating the dataframe 
burberry_df <- data.frame(ID = id_data, Name = names_data, Price = price_data, Colour = colour_data) 

Gibt es eine Möglichkeit, eine Schleife zu erzeugen, so dass ich diesen Code für jedes Element auf der Website voll umfänglich nutzen kann und die Ergebnisse in einem Datenrahmen setzen? Dank

+0

_ "1.4 ... Vorbehaltlich der Bedingungen hier gewähren wir Ihnen eine widerrufbare und nicht exklusive Lizenz für den Zugriff und zur persönlichen Nutzung der Plattformen beschränkt so, dass es nicht das Recht hat, umfassen: ... (c) Verwenden Sie Software Roboter, Spinnen, Crawler oder ähnliches da Ta sammeln und Extraktionswerkzeuge ... "_. Bitte geben Sie zumindest an, wenn Sie andere dazu auffordern, ToS zu verletzen und mögliche rechtliche oder zivilrechtliche Schritte gegen sie einzuleiten. – hrbrmstr

Antwort

0

Sie eine Funktion erstellen können, die eine Eingabe URL akzeptiert und gibt die Informationen einen Datenrahmen von der Webseite gesammelt:

get_page_data <- function(url) { 
    # Read HTML code from the website 
    webpage <- read_html(url) 

    # using css selectors to scrape the ID section 
    id_data_html <- html_nodes(webpage, '.section') 
    #converting the ID to text 
    id_data <- html_text(id_data_html) 
    # Remove irrelevant text 
    id_data <- gsub("Item", "", id_data) 

    # using css selectors to scrape the names section 
    names_data_html <- html_nodes(webpage, '.type-h6') 
    #converting the names to text 
    names_data <- html_text(names_data_html) 
    # Stripping irrelevant text 
    names_data <- gsub("\n\t\t\t\t\t\t\t", "", names_data) 

    # using css selectors to scrape the price section 
    price_data_html <- html_nodes(webpage, '.l2') 
    #converting the price to text 
    price_data <- html_text(price_data_html) 
    # Remove irrelevant text 
    price_data <- gsub("\t", "", price_data) 
    price_data <- gsub("\n", "", price_data) 

    # using css selectors to scrape the colour section 
    colour_data_html <- html_nodes(webpage, '#colour-picker-value') 
    #converting the colour to text 
    colour_data <- html_text(colour_data_html) 

    # creating the dataframe 
    burberry_df <- data.frame(ID = id_data, Name = names_data, Price = price_data, 
           Colour = colour_data) 

    return(burberry_df) 
} 

dann die Funktion zu verwenden, einfach anrufen, während die URL Interesse vorbei:

url <- 'https://uk.burberry.com/fringed-wool-cashmere-patchwork-cardigan-coat-p40612561' 
result <- get_page_data(url)