【问题标题】:Scraping across multiple pages (rvest) R跨多个页面抓取(rvest)R
【发布时间】: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


【解决方案1】:
url<-"https://www.bol.com/nl/l/lichtbronnen/N/14483/?page=3&view=list"
library(rvest)
page<-html_session(url)
total<-html_nodes(page,xpath='//*[@id="js_list_view"]/div[2]/p') %>% html_text()
total<-as.numeric(gsub("[^\\d]+", "", total, perl=TRUE))
all_df<-0
library(data.table)

for(i in 1:ceiling(total/24)){
  url_bol <- paste0('https://www.bol.com/nl/l/lichtbronnen/N/14483/?view=list&page=',i)
  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)
  all_df[i]<-list(bol.df)
}

final<-rbindlist(all_df,fill = TRUE)




View(final)

【讨论】:

  • 您好 Bharath,非常感谢您的回答。然而,这并不能完全解决我的问题,因为我仍然得到前 24 个结果,而我想得到所有结果(16347)。我怎样才能做到这一点?或者我可以简单地输入结果总数: for(i in 1:ceiling(total/24)){ 我还收到以下错误: data.frame 中的错误(Procuct_title = product_title_data, Brand = product_brand_data, : arguments imply不同的行数:25、24 我该如何解决这个问题?
猜你喜欢
  • 2017-03-28
  • 1970-01-01
  • 2022-01-03
  • 2016-08-09
  • 2021-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多