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

查看:92
本文介绍了如何使用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是我最喜欢的餐厅之一-旧金山的taqueria.他们的餐厅ID(可以在评论页面的网址中作为最后一个元素找到)为 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,并返回了基本数据的JSON化结果.看起来像这样:

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 = MyEasy点了超级牛排墨西哥卷饼和男孩,它并没有让人失望!一切...}

这有帮助吗?让我知道您是否还有其他问题.

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

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

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