【发布时间】:2019-02-16 02:16:41
【问题描述】:
我正在尝试使用 rvest 执行网络抓取,以从网上商店抓取特定产品类别的数据。产品结果显示在多个网页上。当我使用我的代码时,我只得到与第一页上的产品相同的前 24 个结果。如何调整我的代码以抓取所有结果页面?
提前致谢!
url_bol <- 'https://www.bol.com/nl/l/lichtbronnen/N/14483'
webpage_bol <- read_html(url_bol,na.strings=" ",header=TRUE)
head(webpage_bol)
product_title_data_html <- html_nodes(webpage_bol, '.product-title')
product_title_data <- html_text(product_title_data_html)
head(product_title_data)
product_title_data<-gsub("\n","",product_title_data)
product_title_data<-gsub(" ","",product_title_data)
head(product_title_data)
length(product_title_data)
product_brand_data_html <- html_nodes(webpage_bol, '.product-creator')
product_brand_data <-html_text(product_brand_data_html)
head(product_brand_data)
product_brand_data<-gsub("\n","",product_brand_data)
product_price_data<-gsub(" ","",product_price_data)
head(product_brand_data)
length(product_brand_data)
product_price_data_html <- html_nodes(webpage_bol, '.promo-price')
product_price_data <- html_text(product_price_data_html)
head(product_price_data)
product_price_data<-gsub("\n","",product_price_data)
product_price_data<-gsub(" ","",product_price_data)
head(product_price_data)
product_price_data
length(product_price_data)
bol.df <- data.frame(Procuct_title = product_title_data, Brand = product_brand_data, Price = product_price_data)
View(bol.df)
【问题讨论】:
-
每个其他页面都有不同的 URL,“page2”、“page3”等。因此您需要在代码中考虑到这一点。
-
您好 Rob,感谢您的反馈。你有什么想法我怎么能做到这一点?我使用了下面 Barath 建议的代码,但我仍然只得到前 24 个结果。
-
我建议使用 for 循环来更改您正在抓取的网站的网址
标签: r web-scraping screen-scraping rvest