R:从Tidyverse中的Google地方信息中提取信息

时间:2019-06-12 16:37:12

标签: r json dplyr tidyverse googleway

我有一个带有位置的数据框,我想在 pipeable 解决方案中从Google地方信息中添加电话号码和网站。

最接近我的方法是使用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()

1 个答案:

答案 0 :(得分:0)

我怀疑那里。这是Google API的两步过程(请参见文档:https://developers.google.com/places/web-service/details):

您需要:

  • (通过“地方搜索”通话)获取唯一的placeid
  • 获取您的placeid的联系方式(通过“地方详情”通话)

如果您自己粘贴url并通过httr执行,则可能对过程有更多控制(Google API以打破更改而著称,R包难以跟上),并且您可以包装难看的自己的函数中编写代码,使调用变得整洁-但最终它将必须是2个API调用。

相关问题