【问题标题】:Web Scraping Iteratively from a WebPage in R从 R 中的网页迭代地抓取网页
【发布时间】:2019-03-21 19:10:43
【问题描述】:

我有一个网页,其中包含一个包含 243 页的表格。每页有 34 行。 第 1 页的url 的结构如下所示。 http://this-site.com/service/?currpage=1.

我想获取 243 页的所有数据并将它们保存在一个 csv 文件中。

到目前为止,我每页使用的代码是

library(XML)
url <- http://this-site.com/service/?currpage=1
service <- as.data.frame(readHTMLTable(url))
head(service)
service <- read_html(url)

如何循环输入从 1 到 243 的数字,以便获取所有页面并下载并将它们写入 csv?

【问题讨论】:

    标签: r xml web-scraping rvest


    【解决方案1】:
    library(tidyverse)
    library(rvest)
    
    pages <- 1:243
    base_url <- "http://this-site.com/service/?currpage="
    urls <- paste0(base_url, pages)
    
    get_table <- function(url) {
      url %>%
        read_html() %>%
        html_table() # might not need this???
    }
    
    results <- sapply(urls, get_table)
    
    bind_rows(reuslts) %>%
      as_data_frame() %>%
      write_csv(path = "some/path/somwhere")
    

    【讨论】:

      猜你喜欢
      • 2021-02-22
      • 2020-07-20
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      相关资源
      最近更新 更多