使用Python查询Yelp API

2024-05-21 22:38:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我在收集yelp的餐厅信息。我有餐厅的名字,我用的是Yelpipi。

我输入了以下内容:

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查询中指定餐厅名称?

另外,我怎样才能把某家餐厅的所有评论都撤出来呢?

谢谢!


Tags: keyfromimporttokenapi信息searchsecret
3条回答

使用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...

这是你要找的餐馆吗:

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

而且,特别是对于pythonyelppi,请求可以构造为

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

它只给了我一小段评论,对于完整的评论,我想你可能要刮网站。看看美丽的人群和破烂。

最后,要回答第一个问题,请尝试在搜索参数中将name替换为term。您可以在此页上找到其他有效搜索参数的列表:

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

通过下面的查询,api给了我正确的业务。

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

祝你好运,快乐刮擦!

较新的Yelp Fusion API(v3)对API的使用方式和返回的信息进行了一些更改。最短的是v2可以通过一个电话获得评论。v3需要两个调用。下面是我是如何让它工作的。你的里程可能不同。

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

相关问题 更多 >