工作目录和网页抓取问题

时间:2021-03-26 15:04:33

标签: r working-directory

当 sapply 函数给我的工作目录带来问题时,我正在编写一个简单的网络抓取脚本。 我声明几天前我在 Windows 10 上更改了用户名,我认为这部分是问题所在。代码如下:

library(string)
library(dplyr)
library(rvest)


cbindPad <- function(...){
  args <- list(...)
  n <- sapply(args,nrow)
  mx <- max(n)
  pad <- function(x, mx){
    if (nrow(x) < mx){
      nms <- colnames(x)
      padTemp <- matrix(0, mx - nrow(x), ncol(x))
      colnames(padTemp) <- nms
      if (ncol(x)==0) {
        return(padTemp)
      } else {
        return(rbind(x,padTemp))
      }
    }
    else{
      return(x)
    }
  }
  rs <- lapply(args,pad,mx)
  return(do.call(cbind,rs))
}



get.SKU <-  function(link_prod){
  page_prod <- read_html(link_prod)
  cod.sku <- page_prod %>% html_nodes(".sku") %>% html_text() 
  return(cod.sku)
}

g <- c("notebook")
page <- 1
trova.gross <- function(page, prodotto) {
  link <- paste0("https://www.galagross.com/page/",page,"/?s=",prodotto,"&post_type=product&product_cat=0")
  
  webpage <- read_html(link)
  
  links_prod <-  webpage %>% html_nodes(".product-title a") %>%  html_attr("href")
  
  name <- tibble( prodotto= prodotto , title <- webpage %>% html_nodes(".product-title a") %>% html_text() )

  name.2_ul <-webpage %>% html_nodes(".woocommerce-product-details__short-description ul") %>% html_text()  %>% tibble()
  
  sku <- data.frame(unlist(sapply(links_prod, FUN=get.SKU , USE.NAMES = FALSE)))
  
  tecno.data <- rbind(tecno.data, cbindPad(name, name.2_ul, sku))
  
}
trova.gross(page = 1, prodotto = g)

这是错误的。我能做什么?预先感谢您的帮助

Error: '' does not exist in current working directory ('C:/Program Files/RStudio')

0 个答案:

没有答案
相关问题