【发布时间】:2015-03-28 05:39:18
【问题描述】:
使用 Yelp v2 API 的搜索结果看起来与您在其网站上找到的结果大不相同
例如在网站上:
http://www.yelp.com/search?find_desc=restaurants&find_loc=Manhattan%2C+NY&ns=1#start=0&sortby=rating
在阅读了他们的文档后,我使用他们的 API 进行了相同的搜索,并使用以下 Python 代码
import json
import argparse
import json
import pprint
import sys
import urllib
import urllib2
import oauth2
HOST = 'api.yelp.com'
PATH = '/v2/search/'
# I put my account's values here, leaving blank for the question
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
TOKEN = ''
TOKEN_SECRET = ''
def main():
url_params = {
'term': 'restaurants',
'location': 'Manhattan,NY',
'sort': 2, # sort by "Highest Rated"
}
url = 'http://{0}{1}?'.format(HOST, PATH)
consumer = oauth2.Consumer(
CONSUMER_KEY,
CONSUMER_SECRET
)
oauth_request = oauth2.Request(
method="GET",
url=url,
parameters=url_params
)
oauth_request.update(
{
'oauth_nonce': oauth2.generate_nonce(),
'oauth_timestamp': oauth2.generate_timestamp(),
'oauth_token': TOKEN,
'oauth_consumer_key': CONSUMER_KEY
}
)
token = oauth2.Token(TOKEN, TOKEN_SECRET)
oauth_request.sign_request(
oauth2.SignatureMethod_HMAC_SHA1(),
consumer,
token
)
signed_url = oauth_request.to_url()
print 'Querying {0} ...'.format(url)
conn = urllib2.urlopen(signed_url, None)
try:
print conn.read()
finally:
conn.close()
我尝试了许多不同的查询参数组合(将位置短语更改为包含空格、没有逗号等,还尝试引入限制和偏移),但没有成功。我做错了什么?
API 查询的结果在这里https://code.stypi.com/cmqnfxuo
【问题讨论】: