【问题标题】:Using rvest to scrape across multiple web pages in r使用 rvest 在 r 中抓取多个网页
【发布时间】:2017-10-25 11:00:21
【问题描述】:

我是 R 新手,一直在做一些网络抓取。我编写了以下代码,它将https://uk.burberry.com/ 中特定商品的 ID、名称、颜色和价格放入数据框中。

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

有没有办法创建一个循环,以便我可以为网站上的每个项目使用此代码并将结果放入数据框中?谢谢

【问题讨论】:

  • "1.4 ... 根据此处的条款,我们授予您访问和个人使用平台的可撤销且非排他性的许可,因此它不包括以下权利: :……(c) 使用软件机器人、蜘蛛、爬虫或类似的数据收集和提取工具……”。请至少说明您何时要求他人违反 ToS 并招致可能针对他们的法律或民事诉讼。

标签: r web-scraping rvest


【解决方案1】:

您可以创建一个函数,该函数接受输入 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)
}

然后要使用该函数,只需在传递感兴趣的 URL 时调用它:

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

【讨论】:

    猜你喜欢
    • 2017-03-01
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 2019-02-16
    • 2022-01-03
    • 1970-01-01
    • 2015-09-06
    相关资源
    最近更新 更多