【问题标题】:Download files in R with URLs that do not end with the file extensions在 R 中下载文件,其 URL 不以文件扩展名结尾
【发布时间】:2021-02-24 07:08:28
【问题描述】:

当 URL 不以“.zip”结尾时,是否有人可以使用 R 下载 GTFS? 例如,这有效:

download.file(url = "http://www.transperth.wa.gov.au/TimetablePDFs/GoogleTransit/Production/google_transit.zip", destfile = "temp.zip")

但以下创建的文件大小合适但无法打开:

download.file(url = "http://transitfeeds.com/p/ptv/497/latest/download", destfile = "temp.zip")

download.file(url = "http://transitfeeds.com/p/ptv/497/latest/download", destfile = "temp")

我怀疑我需要了解一些关于 url 的基本知识,但我不知道在哪里寻找,所以任何指针都会受到赞赏。

干杯,

安东尼

【问题讨论】:

  • 在您提供的transitfeeds 示例中,您确定有什么要下载的吗?它给出了 404 错误。

标签: r url get zip gtfs


【解决方案1】:

您的链接可能是重定向。尝试使用httr 包,如此处所述R download file redirect error

library(httr)

url <- "http://transitfeeds.com/p/ptv/497/latest/download"    
GET(
        url = url,
        write_disk("gtfs.zip"),
        verbose()
    ) -> res

我能够下载文件并打开它。如果可行,您可以删除 verbose() 部分。

【讨论】:

  • 谢谢@kukuk1de,“重定向”是我需要的术语,http 库似乎更适合这项任务,从现在开始,详细对象将派上用场!我欠你一杯啤酒!
  • @Anthony,很高兴听到您的问题已解决。如果您认为我的回答有帮助,请接受它作为解决方案。谢谢你的啤酒:-)
【解决方案2】:

@kukul1de 的回答可以解决问题。

我还注意到,transitfeeds 链接到官方下载 URL。该链接位于右侧的“About This GTFS Feed”下(查看下图):

然后您可以右键单击并选择“复制链接位置”,这将为您提供带有.zip扩展名的官方URL,您可以将其与download.file()结合使用。

但是,这个特定的 URL 链接到的文件实际上是一个 .zip,其中包含许多文件夹,每个文件夹都包含一个不同的 GTFS 文件,而不是 GTFS 格式的 .zip

如果它是一个实际的 GTFS .zip 文件,您可以使用 {gtfstools}{tidytransit} 来读取它,但不幸的是文件格式不允许它。看看吧:

tmp <- tempfile(pattern = "gtfs", fileext = ".zip")

download.file(
    "http://data.ptv.vic.gov.au/downloads/gtfs.zip", 
    destfile = tmp
)

zip::zip_list(tmp)
#>                 filename compressed_size uncompressed_size           timestamp
#> 1                     1/               0                 0 2021-02-22 19:23:20
#> 2                    10/               0                 0 2021-02-22 19:23:20
#> 3  10/google_transit.zip            3231              4011 2021-02-22 19:09:56
#> 4                    11/               0                 0 2021-02-22 19:23:20
#> 5  11/google_transit.zip           29966             32109 2021-02-22 19:10:12
#> 6   1/google_transit.zip         7262254           7625276 2021-02-22 19:01:56
#> 7                     2/               0                 0 2021-02-22 19:23:20
#> 8   2/google_transit.zip         5667379           6269932 2021-02-22 19:03:34
#> 9                     3/               0                 0 2021-02-22 19:23:20
#> 10  3/google_transit.zip         6714271           7782585 2021-02-22 19:05:04
#> 11                    4/               0                 0 2021-02-22 19:23:20
#> 12  4/google_transit.zip        66336783          67508547 2021-02-22 19:23:16
#> 13                    5/               0                 0 2021-02-22 19:23:20
#> 14  5/google_transit.zip        27834469          27962731 2021-02-22 19:06:16
#> 15                    6/               0                 0 2021-02-22 19:23:20
#> 16  6/google_transit.zip        13730731          14172729 2021-02-22 19:09:10
#> 17                    7/               0                 0 2021-02-22 19:23:20
#> 18  7/google_transit.zip           46932             50417 2021-02-22 19:09:24
#> 19                    8/               0                 0 2021-02-22 19:23:20
#> 20  8/google_transit.zip          574316            580906 2021-02-22 19:09:42

假设您要读取 1/ 文件夹中的 GTFS 文件。然后你可以用zip::unzip()解压这个文件:

tmpd <- file.path(tempdir(), "tmp_gtfs")
dir.create(tmpd)

zip::unzip(tmp, files = "1/google_transit.zip", exdir = tmpd)

list.files(tmpd)
#> [1] "1"
list.files(file.path(tmpd, "1"))
#> [1] "google_transit.zip"

使用{gtfstools}{tidytransit} 阅读。这取决于你想对文件做什么:

gtfs_path <- file.path(tmpd, "1", "google_transit.zip")

gt_gtfs <- gtfstools::read_gtfs(gtfs_path)
names(gt_gtfs)
#> [1] "agency"         "routes"         "trips"          "stops"         
#> [5] "calendar"       "calendar_dates" "shapes"         "stop_times"

tt_gtfs <- tidytransit::read_gtfs(gtfs_path)
names(tt_gtfs)
#> [1] "agency"         "routes"         "trips"          "stops"         
#> [5] "calendar"       "calendar_dates" "shapes"         "stop_times"

【讨论】:

  • 感谢您对此进行调查。我的目标是为多个澳大利亚城市获得 GTFS,但不幸的是,墨尔本的官方提要按提供商和/或模式划分。 transitfeeds.com 上的“最新版本”已经合并了墨尔本 GTFS,因此它简化了我脚本中的剩余处理。是的,我一直在使用 tidytransit 库,因为文档比其他 gtfs r 库更有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-07
  • 1970-01-01
  • 1970-01-01
  • 2011-10-01
  • 2010-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多