【发布时间】: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