使用Foursquare API获取场地列表详细信息中的KeyError

问题描述

我是python的新手,我想获得作业场所清单(id和名称)的详细信息(喜欢,等级,提示):

def get_venue_details(venue_id):
    
    #url to fetch data from foursquare api
    url = 'https://api.foursquare.com/v2/venues/{}?&client_id={}&client_secret={}&v={}'.format(
            venue_id,CLIENT_ID,CLIENT_SECRET,VERSION)
    
    # get the data
    results = requests.get(url).json()
    venue_data=results['response']['venue']
    venue_details=[]
    try:
        venue_id=venue_data['id']
        venue_name=venue_data['name']
        venue_likes=venue_data['likes']['count']
        venue_rating=venue_data['rating']
        venue_tips=venue_data['tips']['count']
        venue_details.append([venue_id,venue_name,venue_likes,venue_rating,venue_tips])
    except KeyError:
        pass
        
    column_names=['id','name','likes','rating','tips']
    df = pd.DataFrame(venue_details,columns=column_names)
    return df

然后尝试获取数据:

column_names=['id','tips']
final_venue_details=pd.DataFrame(columns=column_names)
count=1


for row in venue_list.values.tolist():
    ID,Name=row
    try:
        venue_details=get_venue_details(id)
        print(venue_details)
        id,name,likes,rating,tips=venue_details.values.tolist()[0]
    except IndexError:
        print('No data available for id=',ID)
        # assign 0 value for these 
        id,tips=[0]*5
    print('(',count,'/',len(venue_list),')','processed')
    final_venue_details = final_venue_details.append({
                                                'id': id,'name' : name,'likes' : likes,'rating' : rating,'tips' : tips
                                              },ignore_index=True)
    count+=1

我得到以下错误:您能帮我找出原因吗?谢谢!

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-90-ae987cc35f18> in <module>
      8     id,name=row
      9     try:
---> 10         venue_details=get_venue_details(id)
     11         print(venue_details)
     12         id,tips=venue_details.values.tolist()[0]

<ipython-input-88-37f7fb718381> in get_venue_details(id)
     10     # get the data
     11     results = requests.get(url).json()
---> 12     venue_data=results['response']['venue']
     13     venue_details=[]
     14     try:

KeyError: 'venue'

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)