Using R to get download URL by link name

前端 未结 2 1017
灰色年华
灰色年华 2021-01-27 18:00

I\'m trying to use rvest to download a list of files from this site. The file names are regular, but the download URLs don\'t match a pattern (just dozens of digits

相关标签:
2条回答
  • 2021-01-27 18:37

    Slightly different approach.

    Grab all downloadable filenames and URLs:

    library(httr)
    library(rvest)
    library(tidyverse)
    
    pg <- read_html("http://www-air.larc.nasa.gov/cgi-bin/ArcView/actamerica.2016?C130=1")
    
    fils <- html_nodes(pg, xpath=".//a[contains(@href, 'cgi-bin/enzFile')]")
    
    data_frame(
      filename = html_text(fils),
      link = sprintf("http://www-air.larc.nasa.gov%s", html_attr(fils, "href"))
    ) -> xdf
    
    glimpse(xdf)
    ## Observations: 719
    ## Variables: 2
    ## $ filename <chr> "ACTAMERICA-Elevation_C130_20160711_R0.ict", "ACTAMERICA-Elevation_C130_20160715_R0.ict", "ACTAMERI...
    ## $ link     <chr> "http://www-air.larc.nasa.gov/cgi-bin/enzFile?f49DA0512C4E81E3C01FDB44A33CD88AAFE2f7075622d6169722f...
    
    xdf
    ## # A tibble: 719 x 2
    ## filename                                                                                                                                                                                                                                                                           link
    ## <chr>                                                                                                                                                                                                                                                                          <chr>
    ## 1 ACTAMERICA-Elevation_C130_20160711_R0.ict http://www-air.larc.nasa.gov/cgi-bin/enzFile?f49DA0512C4E81E3C01FDB44A33CD88AAFE2f7075622d6169722f414354414d45524943412f323031362f433133305f41495243524146542f444947414e47492e4a4f534855412f414354414d45524943412d456c65766174696f6e5f433133305f32303136303731315f52302e696374
    ## 2 ACTAMERICA-Elevation_C130_20160715_R0.ict http://www-air.larc.nasa.gov/cgi-bin/enzFile?f49DA0512C4E81E3C01FDB44A33CD88AAFE2f7075622d6169722f414354414d45524943412f323031362f433133305f41495243524146542f444947414e47492e4a4f534855412f414354414d45524943412d456c65766174696f6e5f433133305f32303136303731355f52302e696374
    ## 3 ACTAMERICA-Elevation_C130_20160718_R0.ict http://www-air.larc.nasa.gov/cgi-bin/enzFile?f49DA0512C4E81E3C01FDB44A33CD88AAFE2f7075622d6169722f414354414d45524943412f323031362f433133305f41495243524146542f444947414e47492e4a4f534855412f414354414d45524943412d456c65766174696f6e5f433133305f32303136303731385f52302e696374
    ## 4 ACTAMERICA-Elevation_C130_20160719_R0.ict http://www-air.larc.nasa.gov/cgi-bin/enzFile?f49DA0512C4E81E3C01FDB44A33CD88AAFE2f7075622d6169722f414354414d45524943412f323031362f433133305f41495243524146542f444947414e47492e4a4f534855412f414354414d45524943412d456c65766174696f6e5f433133305f32303136303731395f52302e696374
    ## 5 ACTAMERICA-Elevation_C130_20160721_R0.ict http://www-air.larc.nasa.gov/cgi-bin/enzFile?f49DA0512C4E81E3C01FDB44A33CD88AAFE2f7075622d6169722f414354414d45524943412f323031362f433133305f41495243524146542f444947414e47492e4a4f534855412f414354414d45524943412d456c65766174696f6e5f433133305f32303136303732315f52302e696374
    ## 6 ACTAMERICA-Elevation_C130_20160722_R0.ict http://www-air.larc.nasa.gov/cgi-bin/enzFile?f49DA0512C4E81E3C01FDB44A33CD88AAFE2f7075622d6169722f414354414d45524943412f323031362f433133305f41495243524146542f444947414e47492e4a4f534855412f414354414d45524943412d456c65766174696f6e5f433133305f32303136303732325f52302e696374
    ## 7 ACTAMERICA-Elevation_C130_20160725_R0.ict http://www-air.larc.nasa.gov/cgi-bin/enzFile?f49DA0512C4E81E3C01FDB44A33CD88AAFE2f7075622d6169722f414354414d45524943412f323031362f433133305f41495243524146542f444947414e47492e4a4f534855412f414354414d45524943412d456c65766174696f6e5f433133305f32303136303732355f52302e696374
    ## 8 ACTAMERICA-Elevation_C130_20160726_R0.ict http://www-air.larc.nasa.gov/cgi-bin/enzFile?f49DA0512C4E81E3C01FDB44A33CD88AAFE2f7075622d6169722f414354414d45524943412f323031362f433133305f41495243524146542f444947414e47492e4a4f534855412f414354414d45524943412d456c65766174696f6e5f433133305f32303136303732365f52302e696374
    ## 9 ACTAMERICA-Elevation_C130_20160727_R0.ict http://www-air.larc.nasa.gov/cgi-bin/enzFile?f49DA0512C4E81E3C01FDB44A33CD88AAFE2f7075622d6169722f414354414d45524943412f323031362f433133305f41495243524146542f444947414e47492e4a4f534855412f414354414d45524943412d456c65766174696f6e5f433133305f32303136303732375f52302e696374
    ## 10 ACTAMERICA-Elevation_C130_20160801_R0.ict http://www-air.larc.nasa.gov/cgi-bin/enzFile?f49DA0512C4E81E3C01FDB44A33CD88AAFE2f7075622d6169722f414354414d45524943412f323031362f433133305f41495243524146542f444947414e47492e4a4f534855412f414354414d45524943412d456c65766174696f6e5f433133305f32303136303830315f52302e696374
    ## # ... with 709 more rows
    

    Get the ones you care about:

    picarro <- filter(xdf, grepl("PICARRO", filename))
    

    Download them:

    walk2(picarro$link, picarro$filename, download.file)
    ## trying URL 'http://www-air.larc.nasa.gov/cgi-bin/enzFile?f49DA0512C4E81E3C01FDB44A33CD88AAFE2f7075622d6169722f414354414d45524943412f323031362f433133305f41495243524146542f444947414e47492e4a4f534855412f414354414d45524943412d5049434152524f5f433133305f32303136303532375f52422e696374'
    ## Content type 'text/plain' length 1023662 bytes (999 KB)
    ## ==================================================
    ##   downloaded 999 KB
    ## 
    ## trying URL 'http://www-air.larc.nasa.gov/cgi-bin/enzFile?f49DA0512C4E81E3C01FDB44A33CD88AAFE2f7075622d6169722f414354414d45524943412f323031362f433133305f41495243524146542f444947414e47492e4a4f534855412f414354414d45524943412d5049434152524f5f433133305f32303136303731315f52302e696374'
    ## Content type 'text/plain' length 886392 bytes (865 KB)
    ## ==================================================
    ##   downloaded 865 KB
    ## 
    ## trying URL 'http://www-air.larc.nasa.gov/cgi-bin/enzFile?f49DA0512C4E81E3C01FDB44A33CD88AAFE2f7075622d6169722f414354414d45524943412f323031362f433133305f41495243524146542f444947414e47492e4a4f534855412f414354414d45524943412d5049434152524f5f433133305f32303136303731355f52302e696374'
    ## Content type 'text/plain' length 530339 bytes (517 KB)
    ## ==================================================
    ##   downloaded 517 KB
    

    etc.

    0 讨论(0)
  • How about using map2 from purrr to combine two vectors all and html_attr(html_nodes(doc, "a"), "href") and then filter according to file type names

     url <- "http://www-air.larc.nasa.gov/cgi-bin/ArcView/actamerica.2016?C130=1"
    doc <- read_html(url)
    all <- html_text(html_nodes(doc, "td a"))
    href <- html_attr(html_nodes(doc, "a"), "href")
    
    z <- purrr::map2(all, href, function(x, y) data.frame(x, y)) 
    z <- do.call(rbind, z)
    filetype <- "PICARRO"
    z[grep(filetype, z[,1]),]
    
    0 讨论(0)
提交回复
热议问题