【问题标题】:R: Extracting information from Google Places within the TidyverseR:从 Tidyverse 中的 Google Places 中提取信息
【发布时间】:2019-06-12 16:37:12
【问题描述】:

我有一个包含位置的数据框,我想在 pipeable 解决方案中从 Google Places 向它们附加电话号码和网站。

我最接近的做法是使用googleway,取消列出 JSON 并使用正则表达式提取地点 ID,然后对电话号码和电子邮件地址再次执行相同操作。有没有更有针对性的方法?

library(googleway)
library(tidyverse)

set_key("api")

index_no <- c(1,2,3,4)
landmark<- c("Sydney Opera House","Eiffel Tower","Empire State Building","Big Ben")
df <- data.frame(index_no,landmark, stringsAsFactors = F)

df %>%
  rowwise() %>%
  # Place IDs are required for the function beneath
  do(data.frame(., place_id  = unlist(google_places(search_string = .$landmark)))) %>%
  # Place IDs are 27 chars
  filter(grepl("^\\S{27}$", place_id )) %>%
  do(data.frame(., details = unlist(google_place_details(place_id  = .$place_id )))) %>%
  unique() %>%
  # Gets non-Google URls and Phone Numbers
  filter(grepl("(?!.*(google|maps))(^https?.*|^\\+\\d)", details, perl = T )) %>%
  group_by(landmark) %>%
  mutate(seq = 1:n()) %>%
  spread(seq, details) %>%
  rename(phone_number = `1`, website = `2`) %>%
  select(-place_id) %>%
  ungroup()

【问题讨论】:

  • 查看?access_result 看看是否已经有访问器来获取您想要的信息。如果您想从 API 获得特定结果,您可以随时 add itrequest it

标签: r json dplyr tidyverse googleway


【解决方案1】:

我怀疑有。这是 Google API 方面的一个两步过程(请参阅文档:https://developers.google.com/places/web-service/details):

你需要:

  • 获取唯一的placeid(通过地方搜索调用)
  • 获取您的 placeid 的联系方式(通过 Place Details 电话)

如果你自己粘贴你的 url 并通过 httr 执行它,你可能对这个过程有更多的控制(谷歌 API 以破坏性变化而闻名,R 包很难跟上),你可以包装丑陋的部分在您自己的函数中编写代码,使调用更整洁 - 但最终它必须是 2 个 API 调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 2022-10-15
    • 2017-12-30
    • 1970-01-01
    相关资源
    最近更新 更多