代码之家  ›  专栏  ›  技术社区  ›  antecessor

要替换的项目数不是替换长度的倍数。刮削

  •  0
  • antecessor  · 技术社区  · 7 年前

    我知道我的for循环有一些问题,但需要有人指出问题所在。

    这是两页,我想在每个刮100个链接。注意你需要有证件才能进去。但我写这些只是为了看看所有的代码:

    urls <- c("http://cli.linksynergy.com/cli/publisher/links/linkfinder.php?mode=basic&keyword=linux&exact=&any=&exclude=&mid=-1&cat=&sort=&retailprice_sort=&productname_sort=&shortdesp_sort=&categoryname_sort=&keyword_sort=&linklang=pt_BR&currec=1&max=100",
    "http://cli.linksynergy.com/cli/publisher/links/linkfinder.php?mode=basic&keyword=linux&exact=&any=&exclude=&mid=-1&cat=&sort=&retailprice_sort=&productname_sort=&shortdesp_sort=&categoryname_sort=&keyword_sort=&linklang=pt_BR&currec=101&max=100")
    

    我用 rvest

    enlaces <- vector("character", length = length(urls))
    
    for(i in seq_along(urls)){
      Sys.sleep(1)
      derby <- read_html(jump_to(session, urls[i]))
      enlaces[i] <- derby %>%
        html_nodes(".td_auto_left a:nth-child(1)") %>% 
        html_attr('href')
    }
    

    理想情况下,我将得到一个由200个链接组成的向量,其中100个链接是为存储在 urls .

    但是,我得到了错误 Number of items to replace is not a multiple of replacement length .

    我想问题可能是 enlaces 每次迭代只需要一个对象。然而,它创造了100个,不知道如何进行。知道吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   antecessor    7 年前

    我终于通过创建一个 list vector ,并在 for 循环。

    enlaces <- list()
    
    for(i in seq_along(urls)){
      Sys.sleep(1)
      derby <- read_html(jump_to(session, urls[i]))
      enlaces[[i]] <- derby %>%
        html_nodes(".td_auto_left a:nth-child(1)") %>% 
        html_attr('href')
    }