next_page_token无法进行第二次尝试(google_places函数)

时间:2018-05-23 18:21:53

标签: r googleway

我正在寻找所有的星巴克'通过googleway google_places功能在指定位置存储。

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)

这很好,但是,当我尝试获得接下来的20个结果时(结果的第3页)我得到以下错误

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) ------^

此问题已通过软件包的新github版本解决。

后续问题是在尝试以循环方式访问所有网页结果时

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')
}

使用此代码,第三页结果为

$`html_attributions`
list()

$`results`
list()

$`status`
[1] "INVALID_REQUEST"

如果我运行像这样的代码

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)
  }

一切正常。我在这里缺少什么?

0 个答案:

没有答案