如何使用 Yelp 的新 API [英] How to use Yelp's new API

查看:27
本文介绍了如何使用 Yelp 的新 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程还很陌生,所以我确信这是不正确的,但根据我的研究,这是我能做的最好的.谢谢.

I am pretty new to programing so I am sure this is not correct but its the best I can do based on my research. Thanks.

import pandas as pd
import numpy as np
import requests
import yelp
requests.get(https://api.yelp.com/v3/autocomplete?text=del&latitude=37.786882&longitude=-122.399972,headers={'Authorization: Bearer <API KEY that I have>'})

我的菜鸟告诉我这是一本字典

My noob self tells me this is a dictonary

headers={'Authorization: Bearer <API KeY>'}

我知道这是 100% 错误的,所以我真的很想了解更多关于在 Python 中使用 rest API 的信息.我只是作为一个个人项目来做这件事.我的总体目标是能够通过 API 访问 yelps 公共数据.例如,我想获取商家 X 的评论.

I know this is prob 100% wrong so I would really love to learn more about using rest API's in Python. I am just doing this as a personal project. My overall goal is to be able to access yelps public data via API. For example, I want to get the reviews for business X.

更新

requests.get("https://api.yelp.com/v3/autocomplete?text=del&latitude=37.786882&longitude=-122.399972",headers={'Authorization: Bearer <API KEY>'})

我现在收到以下错误

AttributeError: 'set' object has no attribute 'items'

推荐答案

你绝对不会 100% 错@g_altobelli!

You're definitely not 100% wrong @g_altobelli!

让我们以获取企业 X 的评论为例,其中 X 是我最喜欢的餐厅之一——旧金山的 la taqueria.他们的餐厅 ID(可以在他们评论页面的 URL 中作为最后一个元素找到)是 la-taqueria-san-francisco-2.

Let's take the example of getting reviews for business X, where X is one of my favorite restaurants -- la taqueria in San Francisco. Their restaurant id (which can be found in the url of their review page as the last element) is la-taqueria-san-francisco-2.

现在是我们的代码:

您使用请求的想法是正确的,我认为您的参数可能略有偏差.最初有一些标题很有帮助.这是我添加的内容:

You have the right idea using requests, I think your parameters might just be slightly off. It's helpful inititally to have some headers. Here's what I added:

import requests

API_KEY = "<my api key>"

API_HOST = 'https://api.yelp.com'
BUSINESS_PATH = '/v3/businesses/'

然后我创建了一个函数,它接收业务 id 并返回基本数据的 jsonified 结果.看起来像这样:

Then I created a function, that took in the business id and returned a jsonified result of the basic data. That looked like this:

def get_business(business_id):
    business_path = BUSINESS_PATH + business_id
    url = API_HOST + business_path + '/reviews'
    headers = {'Authorization': f"Bearer {API_KEY}"}

    response = requests.get(url, headers=headers)

    return response.json()

最后,我用我的值调用了函数并打印了结果:

Finally, I called the function with my values and printed the result:

results = get_business('la-taqueria-san-francisco-2')
print(results)

我得到的输出是 json,大致如下所示:

The output I got was json, and looked roughly like the following:

{'reviews': [{'id': 'pD3Yvc4QdUCBISy077smYw', 'url': 'https://www.yelp.com/biz/la-taqueria-san-francisco-2?hrid=pD3Yvc4QdUCBISy077smYw和放大器; adjust_creative = hEbqN49-q6Ct_cMosX68Zg和放大器; utm_campaign = yelp_api_v3和放大器; utm_medium = api_v3_business_reviews和放大器; utm_source = hEbqN49-q6Ct_cMosX68Zg ' '文':' 我在这里的第二次.. \倪爱Burito这里有清新的独特滋味..我们订购超级牛排布里托和男孩,它没有让人失望!一切...}

这有帮助吗?如果您还有其他问题,请告诉我.

Does this help? Let me know if you have any more questions.

这篇关于如何使用 Yelp 的新 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆