【问题标题】:Querying Yelp API using Python使用 Python 查询 Yelp API
【发布时间】:2014-09-21 13:31:45
【问题描述】:

我正在尝试在 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 查询中指定餐厅名称?

另外,我将如何提取给定餐厅的所有评论?

谢谢!

【问题讨论】:

    标签: python yelp


    【解决方案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=(',', ': ')))
    

    【讨论】:

      【解决方案2】:

      这是您要找的餐厅吗:

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

      最后一个“/”之后的所有内容都是餐厅的 yelp-id。

      获得 yel​​p-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')
      

      它只给了我一个评论的sn-p,对于完整的评论,我想你可能不得不抓取网站。看看 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)
      

      祝你好运,刮刮乐!

      【讨论】:

        【解决方案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
        

        根据文档,您只能获得 1 篇评论摘录。您可以使用带有从搜索查询中获得的企业 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...
        

        【讨论】:

          猜你喜欢
          • 2012-09-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多