首页 > 解决方案 > 抓取数据时包含两个倍数

问题描述

希望从

https://www.wetterzentrale.de/weatherdata.php?station=260&jaar=2019&maand=1&dag=1

从 2019-01-01 到今天,我还不知道如何编写代码更改 jaar=2019(即 year=2019)、maand=1(即 month=1)和 dag=1(即, day=1) 到所需的日期。

我尝试使用 lapply 作为:

    years <- c("2019", "2020")
    urls <- rbindlist(lapply(years, function(x) {
       url <- paste(https://www.wetterzentrale.de/weatherdata.php?station=260&jaar=2019&maand=1&dag=1, sep = "")
       data.frame(url)
    } ))

因此,这只给出了 2019 年和 2020 年的网址。有没有办法包括月份和日期?

标签: rweb-scraping

解决方案


library(lubridate)

allYourDates <- seq(ymd(20190101), Sys.Date(), by = "days")
urls <- paste("https://www.wetterzentrale.de/weatherdata.php?station=260&jaar=", year(allYourDates)
              , "&maand=", month(allYourDates)
              , "&dag=", day(allYourDates)
              , sep = "")

推荐阅读