使用Python查询Yelp API

0 投票
3 回答
7266 浏览
提问于 2025-04-18 15:17

我正在尝试从Yelp上收集餐厅信息。我有餐厅的名字,并且正在使用yelpapi。

我输入了以下内容:

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

但是我得到了一份包含20个商家的列表,里面没有一个是我想要的那个。那我该怎么在我的API查询中更准确地指定餐厅名字呢?

另外,我该如何提取某个特定餐厅的所有评论呢?

谢谢!

3 个回答

0

term来指定餐厅名称,而不是用name,似乎可以正常工作。

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

根据文档的说明,你只能获取一条评论的摘录。你可以通过使用商家的查询,结合你从搜索查询中得到的商家ID,来获取这条摘录:

>>> 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...
3

新的Yelp Fusion API(版本3)对使用方式和返回的信息做了一些调整。简单来说,版本2只需要一次请求就能获取评论,而版本3则需要进行两次请求。下面是我让它工作的方式,具体情况可能因人而异。

#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=(',', ': ')))
3

这是你要找的餐厅吗:

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

最后一个'/'后面的部分就是餐厅的yelp-id。

一旦你有了yelp-id,就需要用商业API来获取评论。

这里是商业API的文档:

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

获取评论的请求大概是这样的:

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

特别是对于python的yelpapi,构建请求可以这样做:

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

它只给了我评论的一小部分,要获取完整的评论,我觉得你可能需要去抓取网站内容。可以看看BeautifulSoup和Scrapy这两个工具。

最后,针对你第一个问题,试着把name替换成term作为你的搜索参数。你可以在这个页面找到其他有效的搜索参数列表:

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

通过以下查询,API给了我正确的商家信息。

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

祝你好运,抓取愉快!

撰写回答