2017-10-25 7 views
0

나는 R에 익숙하며 일부 웹 스크래핑을하고 있습니다. 나는 특정 품목의 ID, 이름, 색깔 및 가격을 https://uk.burberry.com/에서 자료 구조로두기 위하여 뒤에 오는 부호를 썼다.여러 웹 페이지에 rvesting을 사용하여 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) 

웹 사이트의 모든 항목에이 코드를 사용하여 결과를 데이터 프레임에 넣을 수 있도록 루프를 만드는 방법이 있습니까? 감사합니다

당신은 입력 URL을 허용하고 웹 페이지에서 수집 한 정보와 데이터 프레임을 반환하는 함수 만들 수
+0

_ "1.4 ... 여기 조건에 따라, 우리는 당신에게 취소를 부여하고, 비 독점적 사용권) 소프트웨어 로봇, 거미, 크롤러 또는 유사한 da를 사용하십시오 수집 및 추출 도구 ... "_. 적어도 다른 사람에게 ToS를 위반하고 법적 또는 민사상의 행동을 취할 것을 요구할 때 알려주십시오. – hrbrmstr

답변

0

: 단순히 관심있는 URL을 통과하는 동안 전화

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) 
} 

가 그런 기능을 사용하기를 : (C ... : 액세스가 오른쪽에 포함되지 않도록 제한된 플랫폼의 개인적인 사용을 위해

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