【问题标题】:Large inconsistency between Yelp v2 API queries and equivalent searches on Yelp.comYelp v2 API 查询与 Yelp.com 上的等效搜索之间存在很大的不一致
【发布时间】: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

【问题讨论】:

    标签: python yelp


    【解决方案1】:

    我认为问题可能出在路径上。这里是您的代码

    PATH = '/v2/search/'
    

    所以你的请求网址是这样的。

    http://api.yelp.com/v2/search/?
    

    您的网址路径错误。搜索路径应为:

    PATH = '/v2/search'
    

    请求的url会变成这样:

     http://api.yelp.com/v2/search?term=food&location=San+Francisco
    

    More information

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      • 2019-02-09
      • 2023-01-20
      相关资源
      最近更新 更多