【问题标题】:How to iterate through url in R?如何遍历R中的url?
【发布时间】:2016-12-04 07:55:44
【问题描述】:

我正在尝试遍历具有不同坐标的 url。我要遍历的 url 是 Google Api 文本搜索。

这是一个文本搜索 url 及其参数的示例。我需要迭代位置参数。

https://maps.googleapis.com/maps/api/place/textsearch/json?query=123+main+street&location=42.3675294,-71.186966&radius=10000&key=YOUR_API_KEY

我有一个包含纬度和经度列的数据框。假设它被称为 Cords。

使用语言 R,我想做这样的事情:

for i in 1:length(Cords$lat){
    lat = Cords$lat[i]
    lon = Cords$lon[i] 
    https://maps.googleapis.com/maps/api/place/textsearch/json?query=123+main+street&location=lat,lon&radius=10000&key=YOUR_API_KEY
}

我将每次迭代存储在另一个数据框中,我没有包含该代码。

【问题讨论】:

    标签: r google-maps search google-api logic


    【解决方案1】:

    这只是乞求一个函数包装器。此外,此答案使用 sprintf()paste() 并将 API 视为实际 API 与粘贴的 URL 字符串,并试图帮助避免在脚本中使用裸 API 密钥:

    library(httr)
    library(jsonlite)
    library(purrr)
    
    find_places <- function(query, lon, lat, radius, api_key=Sys.getenv("GOOGLE_API_KEY")) {
    
      res <- GET("https://maps.googleapis.com/maps/api/place/textsearch/json",
                 query=list(query=query,
                            location=sprintf("%s,%s", lat, lon),
                            radius=radius,
                            key=api_key))
    
      fromJSON(content(res, as="text"), flatten=TRUE)
    
    }
    
    df <- data.frame(lat=c(42.3675294, 43.6615, 43.2081),
                     lon=c(-71.186966, -70.2553, 71.5376))
    
    map2(df$lon, df$lat, ~find_places("123 Main St", .x, .y, 10000)) %>% 
      map_df(out, "results") -> places
    
    dplyr::glimpse(places)
    ## Observations: 11
    ## Variables: 18
    ## $ formatted_address               <chr> "123 Main St, Watertown, MA 02472, USA"...
    ## $ icon                            <chr> "https://maps.gstatic.com/mapfiles/plac...
    ## $ id                              <chr> "b2ac1cc162773261571dd4b939b2e6c7ce4cb0...
    ## $ name                            <chr> "123 Main St", "123 Main St", "123 Main...
    ## $ place_id                        <chr> "ChIJ3aqMmgZ444kRgD5YevF7_tc", "EioxMjM...
    ## $ reference                       <chr> "CmRbAAAAo1HUpDIAKtCjc1DCe366g0ehMA_Od5...
    ## $ types                           <list> ["street_address", "street_address", "...
    ## $ geometry.location.lat           <dbl> 42.36753, 43.63520, 43.67802, 42.36753,...
    ## $ geometry.location.lng           <dbl> -71.18697, -70.28722, -70.33530, -71.18...
    ## $ geometry.viewport.northeast.lat <dbl> 42.36771, 43.63521, 43.67804, NA, 41.98...
    ## $ geometry.viewport.northeast.lng <dbl> -71.18689, -70.28721, -70.33529, NA, -8...
    ## $ geometry.viewport.southwest.lat <dbl> 42.36698, 43.63519, 43.67801, NA, 41.98...
    ## $ geometry.viewport.southwest.lng <dbl> -71.18720, -70.28724, -70.33530, NA, -8...
    ## $ photos                          <list> [NULL, NULL, NULL, <2112, <a href="htt...
    ## $ rating                          <dbl> NA, NA, NA, 4.7, 4.6, 4.5, 4.2, NA, 4.5...
    ## $ price_level                     <int> NA, NA, NA, NA, NA, 2, NA, NA, NA, NA, NA
    ## $ opening_hours.open_now          <lgl> NA, NA, NA, FALSE, FALSE, FALSE, NA, NA...
    ## $ opening_hours.weekday_text      <list> [NULL, NULL, NULL, [], [], [], NULL, N...
    

    你也可以使用in this answer提到的包。

    【讨论】:

      【解决方案2】:

      您可以使用paste 函数来构建您的网址。

      Coords <- data.frame(lat = c(1.234, 2.456, 3.456), lon = c(5.678, 6.789, 7.890))
      
      for (i in 1:length(Coords$lat)){
        lat = Coords$lat[i]
        lon = Coords$lon[i] 
        url <- paste('https://maps.googleapis.com/maps/api/place/textsearch/json?query=123+main+street&location=',
      lat, ',', lon, '&radius=10000&key=YOUR_API_KEY', sep = "")
        print(url)
      }
      
      [1] "https://maps.googleapis.com/maps/api/place/textsearch/json?query=123+main+street&location=1.234,5.678&radius=10000&key=YOUR_API_KEY"
      [1] "https://maps.googleapis.com/maps/api/place/textsearch/json?query=123+main+street&location=2.456,6.789&radius=10000&key=YOUR_API_KEY"
      [1] "https://maps.googleapis.com/maps/api/place/textsearch/json?query=123+main+street&location=3.456,7.89&radius=10000&key=YOUR_API_KEY"
      

      【讨论】:

        【解决方案3】:

        @hrbrmstr 暗示/链接到您可以使用我的 googleway 包,它为您包装了 Google Places API(和其他各种 API)

        library(googleway)
        
        df <- data.frame(lat=c(42.3675294, 43.6615, 43.2081),
                         lon=c(-71.186966, -70.2553, 71.5376))
        
        ## your api key goes here
        ##api_key <- xxxx
        
        ## single query
        google_places(search_string = "123 Main St", 
                      location = c(df[1,"lat"], df[1,"lon"]),
                      key = api_key)
        
        
        ## multiple queries
        lst <- apply(df, 1, function(x){
            google_places(search_string = "123 Main St", 
                          location = c(x["lat"], x["lon"]),
                          key = api_key)
        })
        

        lst 现在是所有结果的列表,因此例如我们可以查看所有地址

        lapply(lst, function(x){ x[["results"]][["formatted_address"]] })
        # [[1]]
        # [1] "123 Main St, Watertown, MA 02472, USA"
        # 
        # [[2]]
        # [1] "123 Main St, South Portland, ME 04106, USA" "123 Main St, Westbrook, ME 04092, USA"     
        # 
        # [[3]]
        # [1] "123 Main St, Watertown, MA 02472, United States
        

        【讨论】:

          猜你喜欢
          • 2021-11-19
          • 1970-01-01
          • 2020-09-21
          • 1970-01-01
          • 2018-07-07
          • 2014-12-05
          • 1970-01-01
          • 2016-05-27
          • 1970-01-01
          相关资源
          最近更新 更多