Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rvest网络垃圾处理有限结果(R)_R_Web Scraping_Rvest - Fatal编程技术网

Rvest网络垃圾处理有限结果(R)

Rvest网络垃圾处理有限结果(R),r,web-scraping,rvest,R,Web Scraping,Rvest,我是个新手,尝试过几种方法在多个页面上执行rvest。不知何故,它仍然不起作用,我只得到了15个结果,而不是在这个类别中列出的207个产品。我做错了什么 library(rvest) all_df<-0 library(data.table) for(i in 1:5){ url_fonq <- paste0("https://www.fonq.nl/producten/categorie-lichtbronnen/?p=",i,sep="") webpage_fonq &

我是个新手,尝试过几种方法在多个页面上执行rvest。不知何故,它仍然不起作用,我只得到了15个结果,而不是在这个类别中列出的207个产品。我做错了什么

library(rvest)
all_df<-0
library(data.table)

for(i in 1:5){
  url_fonq <- paste0("https://www.fonq.nl/producten/categorie-lichtbronnen/?p=",i,sep="")
  webpage_fonq <- read_html(url_fonq)
  head(webpage_fonq)

  product_title_data_html <- html_nodes(webpage_fonq, '.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_price_data_html <- html_nodes(webpage_fonq, '.product-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)
  fonq.df <- data.frame(Procuct_title = product_title_data, Price = product_price_data)
  all_df <-list(fonq.df)
  }

final2<-rbindlist(all_df,fill = TRUE)

View(final2)
库(rvest)

问题在于,你只保留了从网站最后一页刮取的数据,因此你只存储了最后15种产品

因此,不要在每次迭代中覆盖all_df变量

all_df <- list(fonq.df)
以下是我的完整解决方案:

library(rvest)
all_df <- list()
library(dplyr)

for(i in 1:5){

  url_fonq <- paste0("https://www.fonq.nl/producten/categorie-lichtbronnen/?p=",i,sep="")
  webpage_fonq <- read_html(url_fonq)
  head(webpage_fonq)

  product_title_data_html <- html_nodes(webpage_fonq, '.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_price_data_html <- html_nodes(webpage_fonq, '.product-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)
  fonq.df <- data.frame(Procuct_title = product_title_data, Price = product_price_data)
  all_df <-bind_rows(all_df, fonq.df)
}

View(all_df)
库(rvest)
全方位
library(rvest)
all_df <- list()
library(dplyr)

for(i in 1:5){

  url_fonq <- paste0("https://www.fonq.nl/producten/categorie-lichtbronnen/?p=",i,sep="")
  webpage_fonq <- read_html(url_fonq)
  head(webpage_fonq)

  product_title_data_html <- html_nodes(webpage_fonq, '.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_price_data_html <- html_nodes(webpage_fonq, '.product-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)
  fonq.df <- data.frame(Procuct_title = product_title_data, Price = product_price_data)
  all_df <-bind_rows(all_df, fonq.df)
}

View(all_df)