【问题标题】:Yelp Fusion API with R带有 R 的 Yelp Fusion API
【发布时间】:2019-03-04 08:37:12
【问题描述】:

Yelp Fusion API 允许您在 R 中一次提取 50 行,但您可以通过更改偏移量来循环执行此操作。如果我使用

  library(yelpr)
     Key <- ***
     NYC_Restaurants <- business_search(api_key = key,
                                    location = 'New York',
                                    term = "restaurants",
                                    offset = 0,
                                    limit = 50)

每次我可以获得我想要的所有数据时,通过添加 50 来不断增加偏移量。不过我知道应该有办法写一个循环。

我试过了

restaurant_function <- function(location, term){
    business_search(api_key = key,
                    location = location,
                    term = term,
                    offset = offset,
                   limit = 50)
                  }

NYC_Restaurants <- for (offset in seq(0,1000,50)) {
                        restaurant_function('New York','restaurants')
                        }

它只是返回一个空集。对我做错的任何帮助和理解将不胜感激。谢谢你。

【问题讨论】:

    标签: r api for-loop yelp


    【解决方案1】:

    我没有 API 密钥,所以我无法复制它,但文档看起来很清楚:Yelp business_search

    我认为问题出在你的 for 循环上,而你没有在函数中提供偏移量。

    key = "api_key"
    
    restaurant_function <- function(key, location, term, offset){
      business_search(api_key = key,
                      location = location,
                      term = term,
                      offset = offset,
                      limit = 50)
    }
    
    NYC_Restaurants <- data.frame()
    
    for (offset_val in seq(0,1000,50)) {
      temp <- restaurant_function(key, 'New York','restaurants', offset_val)
      NYC_Restaurants <- rbind(NYC_Restaurants, temp)
    }
    
    print(NYC_Restaurants)
    

    您也可以尝试将 offset 设置为全局变量:

    restaurant_function <- function(location, term){
      business_search(api_key = key,
                      location = location,
                      term = term,
                      offset = offset,
                      limit = 50)
    }
    
    NYC_Restaurants <- data.frame()
    
    for (offset_val in seq(0,1000,50)) {
      offset <<- offset_val
      temp <- restaurant_function('New York','restaurants')
      NYC_Restaurants <- rbind(NYC_Restaurants, temp)
    }
    
    print(NYC_Restaurants)
    

    希望这会有所帮助。

    【讨论】:

    • 我采纳了你的第一个建议,它正在迭代,但 R 在 rbind 上返回了重复的 row.names 错误。有没有办法在每个连续循环中的行名上设置 n+1?它从 1 变为 50,然后再次执行此操作,因此它试图绑定到具有相同行 id 的数据帧,这是不允许的。我需要它在第二个循环中从 51 变为 100,依此类推。
    • business_search 是否返回数据框?如果是这样,请尝试在 rbind 之前使用 dplyr::bind_rows 或删除 row_id temp &lt;- restaurant_function('New York','restaurants') %&gt;% select(-row_id)
    【解决方案2】:

    使用 creativename 的功能(谢谢):

    restaurant_function <- function(key, location, term, offset){
      business_search(api_key = key,
                      location = location,
                      term = term,
                      offset = offset,
                      limit = 50)
    }
    

    我与一位同事合作获得了一个循环,它将 1000 个结果返回到数据框中:

    NYC_Restaurants <- data.frame()
    
    for (offset_val in seq(0,1000,50)) {
      print(offset_val)
      temp <- restaurant_function(key, 'New York', 'restaurants', offset_val) %>% 
        .$businesses %>% 
        mutate(categories = map(categories, flatten_chr)) %>% 
        mutate(categories = lapply(categories, paste, collapse = ", ") %>% unlist()) %>% 
        mutate(latitude = .$coordinates$latitude) %>% 
        mutate(longitude = .$coordinates$longitude) %>% 
        select(-coordinates) %>% 
        mutate(transactions = lapply(transactions, paste, collapse = ", ") %>% unlist()) %>% 
        mutate(address = .$location$display_address)  %>% 
        mutate(address = lapply(address, paste, collapse = ", ") %>% unlist()) %>% 
        select(-location)
      NYC_Restaurants <- bind_rows(NYC_Restaurants, temp)
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-19
      • 1970-01-01
      • 2022-12-26
      相关资源
      最近更新 更多