next_page_token not working on second attempt (google_places function)

左心房为你撑大大i 提交于 2019-12-24 17:42:14

问题


I am looking for all starbucks' stores in a given location through googleway's google_places function.

library(googleway)
res <- google_places(search_string = 'starbucks in Sao Paulo SP',
                       key = my_key)
# get second page of results
token <- res$next_page_token
res_next1   <- google_places(search_string = 'starbucks in Sao Paulo SP',
                         page_token    = token,
                         key = my_key)

This works fine, however, when I attempt to get the next 20 results (page 3 of results) I get the following error

token2<- res_next1$next_page_token
res_next2 <- google_places(search_string = 'starbucks in Sao Paulo SP',
                     page_token    = token2,
                     key = my_key)
Error: lexical error: invalid char in json text.
                                   https://maps.googleapis.com/map
                 (right here) ------^

This problem was solved with the new github version of the package.

A subsequent problem was when trying to access all page results in a loop

lool = list()
# problem happens when searches[i] = 'starbucks in Sao Paulo, Sao Paulo'
for(i in 1:length(searches)){

  j = 1 # page counter
  res <- google_places(search_string = searches[i],
                       key = key)

  if(is.null(res$results$name)) {cat(i, 'null\n'); next}

  lool[[paste0(i, '-', j)]] <- data.frame(title   = res$results$name,
                                          address = res$results$formatted_address)
  token = res$next_page_token

  while(!is.null(token)){
    j = j + 1
    res_n <- google_places(search_string = searches[i],
                           page_token    = token,
                           key = key)

    lool[[paste0(i, '-', j)]] <- data.frame(title   = res_n$results$name,
                                            address = res_n$results$formatted_address)
    token <- res_n$next_page_token

  }
  aa = res$status
  cat(i, j, aa, '\n')
}

With this code, the third page results in

$`html_attributions`
list()

$`results`
list()

$`status`
[1] "INVALID_REQUEST"

While if I run the code like this

token <- res$next_page_token

  if(!is.null(token)){
    j     <- j + 1
    res_2 <- google_places(search_string = searches[i],
                           page_token    = token,
                           key = joao_key)

    lool[[paste0(i, '-', j)]] <- data.frame(title   = res_2$results$name,
                                            address = res_2$results$formatted_address)
    token2 <- res_2$next_page_token
    #print(j)
  }
  if(!is.null(token2)){
    j = j + 1
    res_3 <- google_places(search_string = searches[i],
                           page_token    = token,
                           key = joao_key)

    lool[[paste0(i, '-', j)]] <- data.frame(title   = res_3$results$name,
                                            address = res_3$results$formatted_address)
    token3 <- res_3$next_page_token
    #print(j)
  }
  if(!is.null(token3)){
    j = j + 1
    res_4 <- google_places(search_string = searches[i],
                           page_token    = token,
                           key = joao_key)

    lool[[paste0(i, '-', j)]] <- data.frame(title   = res_4$results$name,
                                            address = res_4$results$formatted_address)
    token4 <- res_4$next_page_token
    #print(j)
  }

all works fine. What am I missing here?

来源:https://stackoverflow.com/questions/50495033/next-page-token-not-working-on-second-attempt-google-places-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!