简体   繁体   English

如何从 Python 中的 Yelp API 请求中获得 3 条评论?

[英]How to get exactly 3 reviews from Yelp API request in python?

Hi I am trying to extract exactly 3 reviews for a list of businesses that I already have business ids.嗨,我正在尝试为我已经拥有企业 ID 的企业列表提取 3 条评论。 So far I have written this much of the code.到目前为止,我已经编写了这么多代码。 I need a function that would specifically retrieve business review from yelp API.我需要一个专门从 yelp API 检索业务评论的函数。 Please let me know what function can be create to add to this that would retrieve the same.请让我知道可以创建什么函数来添加到这个函数中来检索相同的函数。 thanks谢谢

""" """

from __future__ import print_function
#import yelpSDK
import json
#from pandas import *
#import pdb
import pandas as pd
import datetime
#import geocoder
#from geopy.geocoders import Nominatim


import argparse
import json
import pprint
import requests
import sys
import urllib


# This client code can run on Python 2.x or 3.x.  Your imports can be
# simpler if you only need one of those.
try:
    # For Python 3.0 and later
    from urllib.error import HTTPError
    from urllib.parse import quote
    from urllib.parse import urlencode
except ImportError:
    # Fall back to Python 2's urllib2 and urllib
    from urllib2 import HTTPError
    from urllib import quote
    from urllib import urlencode
# Yelp Fusion no longer uses OAuth as of December 7, 2017.
# You no longer need to provide Client ID to fetch Data
# It now uses private keys to authenticate requests (API Key)
# You can find it on
# https://www.yelp.com/developers/v3/manage_app
API_KEY= ''


# API constants, you shouldn't have to change these.
API_HOST = 'https://api.yelp.com'
SEARCH_PATH = '/v3/businesses/search'
BUSINESS_PATH = '/v3/businesses/'  # Business ID will come after slash.
BUSINESS_MATCHES_PATH = '/v3/businesses/matches'

# Defaults for our simple example.
DEFAULT_TERM = 'dinner'
DEFAULT_LOCATION = 'San Francisco, CA'
SEARCH_LIMIT = 3


def request(host, path, api_key, url_params=None):
    """Given your API_KEY, send a GET request to the API.
    Args:
        host (str): The domain host of the API.
        path (str): The path of the API after the domain.
        API_KEY (str): Your API Key.
        url_params (dict): An optional set of query parameters in the request.
    Returns:
        dict: The JSON response from the request.
    Raises:
        HTTPError: An error occurs from the HTTP request.
    """
    url_params = url_params or {}
    url = '{0}{1}'.format(host, quote(path.encode('utf8')))
    headers = {
        'Authorization': 'Bearer %s' % api_key,
    }

    print(u'Querying {0} ...'.format(url))

    response = requests.request('GET', url, headers=headers, params=url_params)
    return response.json()

def search_matches(api_key, business):
    """Query the Search API by a search term and location.
    Args:
        term (str): The search term passed to the API.
        location (str): The search location passed to the API.
    Returns:
        dict: The JSON response from the request.
    """

    url_params = {
        'name': business['name'],#replace(' ', '+'),
        'address1': business['address1'],#.replace(' ', '+'),
        'city': business['city'],#.replace(' ', '+'),
        'state': business['state'],#.replace(' ', '+'),
        'zip_code': business['zip_code'],#.replace(' ', '+'),
        'country': business['country']#.replace(' ', '+')
    }
    return request(API_HOST, BUSINESS_MATCHES_PATH, api_key, url_params=url_params)

def get_business(API_KEY, business_id):
    """Query the Business API by a business ID.
    Args:
        business_id (str): The ID of the business to query.
    Returns:
        dict: The JSON response from the request.
    """
    business_path = BUSINESS_PATH + business_id

    return request(API_HOST, business_path, API_KEY)


def get_business_id(business):
    #import pdb; pdb.set_trace()
    try:
        business_id = search_matches(API_KEY, business)

        b_id = business_id['businesses'][0]['id']
    except:
        b_id = 'None'
    return b_id

Are you looking for something like this?你在寻找这样的东西吗?

def get_three(id1, id2, id3):
    return [get_business(API_KEY, id) for id in [id1, id2, id3]]

Yelp Reviews API Endpoint returns up to 3 reviews. Yelp 评论 API 端点最多返回 3 条评论。 There's no need to filter anything, just use it and it'll return at max 3 results.无需过滤任何内容,只需使用它即可返回最多 3 个结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM