我在收集关于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查询中指定餐厅名称?
另外,我该如何为一家给定的餐厅提取所有的评论呢?
谢谢!
发布于 2014-07-30 03:33:26
这是你要找的餐馆吗?
http://www.yelp.com/biz/neptune-oyster-boston?最后的“/”之后的每一件事都是餐馆的叫喊声。
一旦您拥有了yelp-id,您就需要使用业务api来获得评论。
以下是业务api的文档
http://www.yelp.com/developers/documentation/v2/business你想得到评论的请求如下:
http://api.yelp.com/v2/business/neptune-oyster-boston而且,特别是对于python,请求可以构造为
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)祝你好运,祝你刮风愉快!
发布于 2017-08-29 04:08:28
较新的Yelp (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=(',', ': ')))发布于 2014-07-30 03:39:17
使用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...https://stackoverflow.com/questions/25027823
复制相似问题