Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script to collect search count data for various terms in Yelp across top 60 US cities
require 'oauth'
require 'json'
require 'pp'
require 'csv'
consumer_key = '#YOURS'
consumer_secret = '#YOURS'
token = '#YOURS'
token_secret = '#YOURS'
api_host = ''
consumer =, consumer_secret, {:site => "http://#{api_host}"})
access_token =, token, token_secret)
cities = [
"New+York", "Los+Angeles", "Chicago", "Houston", "Philadelphia", "Pheonix", "San+Antonio", "San+Diego", "Dallas", "San+Jose", "Austin", "Jacksonville",
"Indianapolis", "San+Francisco", "Columbus", "Fort+Worth", "Charlotte", "Detroit", "El+Paso", "Memphis", "Boston", "Seattle", "Denver", "Washington",
"Nashville", "Baltimore", "Louisville", "Portland", "Oklahoma+City", "Milwaukee", "Las+Vegas", "Albuquerque", "Tucson", "Fresno", "Sacramento",
"Long+Beach", "Kansas+City", "Mesa", "Virginia+Beach", "Atlanta","Colorado+Springs", "Raleigh", "Omaha", "Miami", "Oakland", "Tulsa", "Minneapolis",
"Cleveland", "Wichita", "Arlington", "New+Orleans", "Bakersfield", "Tampa", "Honolulu", "Anaheim", "Aurora", "Santa+Ana", "St.+Louis,MA", "Riverside",
"Corpus+Christi", "Pittsburgh"
segments = [
"fitness+class", "bootcamp", "crossfit+gym", "yoga+class", "pilates+class", "martial+arts+class", "spin+class",
"biking+class", "boxing", "ballet+class", "barre+class", "yoga", "pilates"
]"yelp_two.csv", "wb") do |csv|
row =
row << "City"
for y in 0..(segments.length-1)
row << segments[y]
csv << row
for z in 0..(cities.length-1)
city = cities[z]
row =
row << city
pp city
for i in 0..(segments.length-1)
item = segments[i]
path = "/v2/search?term=#{item}&location=#{city}"
result = JSON.parse(access_token.get(path).body)
count = result["total"]
pp count
row << count
csv << row
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment