从 R 中的 Google 地图中抓取城市和州
Scraping City and State from Google Maps in R
我正在从事一个需要一些基本位置信息但相关信息很少的项目。我正在尝试在 R 中编写一个函数或一系列行来获取地标的名称,在 Google 地图搜索中使用该名称,然后抓取网站以提取地标的城市和州.
例子
landmark.data <- data.frame(landmark.name = c("Packer Stadium", "University of Chicago", "Disney World"))
findCityState(landmark.data) # Insert amazing function here
landmark.city landmark.state
1 Green Bay WI
2 Chicago IL
3 Orlando FL
我尝试过使用许多包,如 rvest
和 curl
,但我在 city/state 的 CSS 选择中遇到了困难,并且老实说,我什至不确定这是否可能。
您可能建议的解决此问题的任何其他方法也将不胜感激,但如果 R 中有这种方法,那就太理想了。
谢谢!!
鉴于 Google 有一个 API 供您访问,可能不需要抓取数据。
我的 Googleway 包为其中的大多数提供了便利的功能,其中之一是地理编码
library(googleway)
## you need an api key to use their service
key <- 'your_api_key'
landmark.data <- c("Packer Stadium", "University of Chicago", "Disney World")
lst_result <- lapply(landmark.data, function(x){
google_geocode(x, key = key)
})
你可能遇到的问题是没有'standard'地址格式,所以如果你想得到具体的地址信息,就得机灵一点
例如,这是为您的前两个搜索项列出的地址
lapply(lst_result, function(x){
x[['results']][['address_components']]
})
# [[1]]
# [[1]][[1]]
# long_name short_name types
# 1 1265 1265 street_number
# 2 Lombardi Avenue Lombardi Ave route
# 3 Green Bay Green Bay locality, political
# 4 Brown County Brown County administrative_area_level_2, political
# 5 Wisconsin WI administrative_area_level_1, political
# 6 United States US country, political
# 7 54304 54304 postal_code
#
#
# [[2]]
# [[2]][[1]]
# long_name short_name types
# 1 5801 5801 street_number
# 2 South Ellis Avenue S Ellis Ave route
# 3 South Side South Side neighborhood, political
# 4 Chicago Chicago locality, political
# 5 Chicago Chicago administrative_area_level_3, political
# 6 Cook County Cook County administrative_area_level_2, political
# 7 Illinois IL administrative_area_level_1, political
# 8 United States US country, political
# 9 60637 60637 postal_code
虽然我们在这里,但让我们看看地图上实际给我们的结果是什么
mapKey <- symbolix.utils::mapKey()
lst_coordinates <- lapply(lst_result, function(x){
x[['results']][['geometry']][['location']]
})
coordinates <- do.call('rbind', lst_coordinates)
google_map(key = mapKey) %>%
add_markers(coordinates)
我正在从事一个需要一些基本位置信息但相关信息很少的项目。我正在尝试在 R 中编写一个函数或一系列行来获取地标的名称,在 Google 地图搜索中使用该名称,然后抓取网站以提取地标的城市和州.
例子
landmark.data <- data.frame(landmark.name = c("Packer Stadium", "University of Chicago", "Disney World"))
findCityState(landmark.data) # Insert amazing function here
landmark.city landmark.state
1 Green Bay WI
2 Chicago IL
3 Orlando FL
我尝试过使用许多包,如 rvest
和 curl
,但我在 city/state 的 CSS 选择中遇到了困难,并且老实说,我什至不确定这是否可能。
您可能建议的解决此问题的任何其他方法也将不胜感激,但如果 R 中有这种方法,那就太理想了。
谢谢!!
鉴于 Google 有一个 API 供您访问,可能不需要抓取数据。
我的 Googleway 包为其中的大多数提供了便利的功能,其中之一是地理编码
library(googleway)
## you need an api key to use their service
key <- 'your_api_key'
landmark.data <- c("Packer Stadium", "University of Chicago", "Disney World")
lst_result <- lapply(landmark.data, function(x){
google_geocode(x, key = key)
})
你可能遇到的问题是没有'standard'地址格式,所以如果你想得到具体的地址信息,就得机灵一点
例如,这是为您的前两个搜索项列出的地址
lapply(lst_result, function(x){
x[['results']][['address_components']]
})
# [[1]]
# [[1]][[1]]
# long_name short_name types
# 1 1265 1265 street_number
# 2 Lombardi Avenue Lombardi Ave route
# 3 Green Bay Green Bay locality, political
# 4 Brown County Brown County administrative_area_level_2, political
# 5 Wisconsin WI administrative_area_level_1, political
# 6 United States US country, political
# 7 54304 54304 postal_code
#
#
# [[2]]
# [[2]][[1]]
# long_name short_name types
# 1 5801 5801 street_number
# 2 South Ellis Avenue S Ellis Ave route
# 3 South Side South Side neighborhood, political
# 4 Chicago Chicago locality, political
# 5 Chicago Chicago administrative_area_level_3, political
# 6 Cook County Cook County administrative_area_level_2, political
# 7 Illinois IL administrative_area_level_1, political
# 8 United States US country, political
# 9 60637 60637 postal_code
虽然我们在这里,但让我们看看地图上实际给我们的结果是什么
mapKey <- symbolix.utils::mapKey()
lst_coordinates <- lapply(lst_result, function(x){
x[['results']][['geometry']][['location']]
})
coordinates <- do.call('rbind', lst_coordinates)
google_map(key = mapKey) %>%
add_markers(coordinates)