Question

I'm trying to collect restaurant information on yelp. I have the restaurant name, and am using yelpapi.

I typed in the following:

from yelpapi import YelpAPI
yelp_api = YelpAPI(<key>, <secret>, <token>, <token secret>)
search_results = yelp_api.search_query(name = 'Neptune Oyster', location='Boston, MA'),

But ended up with a list of 20 businesses, none of which are the right one. How should I specify restaurant name in my API query?

Also, how would I go about pulling out all of the reviews for a given restaurant?

Thanks!

Was it helpful?

Solution

Is this the restaurant you're looking for:

http://www.yelp.com/biz/neptune-oyster-boston?

Everything after the last '/' is the restaurant's yelp-id.

Once you have the yelp-id you need to use the business api to get reviews

Here's the documentation for the business api

http://www.yelp.com/developers/documentation/v2/business

Your request to get the reviews would be like this:

http://api.yelp.com/v2/business/neptune-oyster-boston

and, specifically for the python yelpapi, the request can be constructed as

yelp_api.business_api('neptune-oyster-boston')

It only gave me a snippet of the review, for the full review, I think you might have to scrape the website. Look into BeautifulSoup and Scrapy.

Finally, to answer your first question, try replacing name with term in your search parameter. You can find a list of other valid search parameters on this page:

http://www.yelp.com/developers/documentation/v2/search_api

With the following query,the api gave me the right business.

yelp_api.search_query(term='neptune oysters', location='boston', limit=1)

Good luck and happy scraping!

OTHER TIPS

The newer Yelp Fusion API (v3) has resulted in some changes to how the API is used and the information returned. The short of it is v2 could get reviews with a single call. v3 requires two calls. Below is how I was able to get it to work. Your mileage may vary.

#Finding reviews for a particular restaurant
import http.client
import json
import urllib

headers = {
'authorization': "Bearer <access_token>",
'cache-control': "no-cache",
'postman-token': "<token>"
}
#need the following parameters (type dict) to perform business search. 
params = {'name':'Neptune oyster', 'address1':'63 Salem St.', 'city':'Boston', 'state':'MA', 'country':'US'}

param_string = urllib.parse.urlencode(params)
conn = http.client.HTTPSConnection("api.yelp.com")
conn.request("GET", "/v3/businesses/matches/best?"+param_string, headers=headers)

res = conn.getresponse()
data = res.read()
data = json.loads(data.decode("utf-8"))

b_id = data['businesses'][0]['id']

r_url = "/v3/businesses/" + b_id + "/reviews"    #review request URL creation based on business ID
conn.request("GET",r_url,headers=headers)
rev_res = conn.getresponse()     #response and read functions needed else error(?)
rev_data = rev_res.read()
yelp_reviews = json.loads(rev_data.decode("utf-8"))

print(json.dumps(yelp_reviews, indent=3, separators=(',', ': ')))

Specifying the restaurant name using term instead of name seems to work.

from yelpapi import YelpAPI
yelp_api = YelpAPI(key, secret, token, token_secret)
search_results = yelp_api.search_query(term='Neptune Oyster', location='Boston, MA')

>>> for business in search_results['businesses']:
...     print business['name']
... 
Neptune Oyster
Island Creek Oyster Bar
B & G Oysters
Rabia's
Union Oyster House
Pauli's
James Hook & Co
Row 34
Atlantic Fish Company
Mare
The Oceanaire Seafood Room
Alive & Kicking Lobsters
The Daily Catch
Yankee Lobster Fish Market
The Barking Crab
Boston Chowda Co.
Legal Sea Foods
Salty Dog Seafood Grille & Bar
Legal Sea Foods
Legal Sea Foods

According to the documentation, you are limited to 1 excerpt of a review. You can get that using a business query with the id of the business that you got from the search query:

>>> search_results = yelp_api.search_query(limit=1, term='Neptune Oyster', location='Boston, MA')
>>> if search_results['businesses'][0]['name'] == 'Neptune Oyster':
...     business_id = search_results['businesses'][0]['id']
...     business_results = yelp_api.business_query(id=business_id)
...     for review in business_results['reviews']:
...         print review['excerpt']
... 
Price/Food
- Waited almost two hours for this place! I talked to some people that were waiting in line and they were all raving that Neptune is the BEST...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top