Why are keyword searches acting like name searches

2019-06-03 15:41发布

I am working on a public health project where I'm scraping an entire state for tobacco vaping shops. I've been experimenting using the google places API webservice, specifically the Nearby and Text methods. Below is some quick R code (needs some error checking, but runs) to show you the two versions of what I'm doing.

I'm finding that keyword searches seem to be behaving like name searches - my results for "vape" in either Nearby Search Requests or Text Search Requests are being waaay too strict, giving me only, for instance "The Vape Escape", "Cloud Vape Lounge" and "Juice Vapeorium" - but when I go to the same location in google maps by hand and search "vape" or "vape near me", I get over a dozen, better results, including things like "Vapor girl" and "Hazmat" that don't have "vape" in the name.

Any ideas what I'm doing wrong, that the API seems to be interpreting my keyword requests as name requests?

library(RCurl)
library(RJSONIO)

# NOTE: Radar search may be better here, eventually. https://developers.google.com/places/web-service/search
place_url = function(lat, lon, rad = 5000, kw, return.call = "json", sensor = "false") {
  root = "https://maps.googleapis.com/maps/api/place/nearbysearch/"
  api_key = "MY KEY WENT HERE"
  u <- paste(root, return.call, "?location=", lat, ",", lon, "&radius=", rad, "&keyword=", kw, "&key=", api_key, sep = "")
  return(URLencode(u))
}

text_url = function(lat, lon, rad = 5000, kw, return.call = "json", sensor = "false") {
  #10X multiplier! Be careful!
  root = "https://maps.googleapis.com/maps/api/place/textsearch/"
  api_key = "MY KEY WENT HERE"
  u <- paste(root, return.call, "?location=", lat, ",", lon, "&radius=", rad, "&query=", kw, "&key=", api_key, sep = "")
  return(URLencode(u))
}


place_scrape <- function(keyword, lat, lon, radius=35405.6, verbose=TRUE) {
  # radius=35405.6: ~=22 miles. circular radius means ~11x11 mile square w/ overlap. Treat like 10x10, dump dups.
  df = data.frame(name=character(), lat=double(), lon=double(), id=character(), vicinity=character()) #for starters

  if(verbose) cat("Looking for '", keyword,"' around lat", lat, "and lon", lon, "\n")
  #lat = 35.917217; lon = -79.054554; radius = 35405.6; keyword = "vape near me"; verbose=T
  u=place_url(lat,lon, radius, keyword) 
  #u=text_url(lat,lon, radius, keyword) 
  doc <- getURL(u)
  x <- fromJSON(doc,simplify = FALSE)
  length(x)
  if(verbose) cat("Found", length(x), "results.\n")

  for (i in 1:length(x)){
    result = data.frame(name=x$results[[i]]$name, 
                        lat= x$results[[i]]$geometry$location$lat, lon = x$results[[i]]$geometry$location$lng, 
                        id = x$results[[i]]$id, 
  #                      vicinity=x$results[[i]]$formatted_address) #text version
                        vicinity=x$results[[i]]$vicinity) #place version
    df = rbind(df, result)
  }
  if(verbose) print(df)
  return(df)
}

# 35.917217, -79.054554 is chapel hill
results = place_scrape("vape", 35.917217, -79.054554)
results = place_scrape("vapor", 35.917217, -79.054554)
results = place_scrape("vapes", 35.917217, -79.054554)


#scrape maryland: from 40N to 38.5. ~360 miles across by 

0条回答
登录 后发表回答