KeyError:使用Python Instagram API客户端的“数据" [英] KeyError: 'data' with Python Instagram API client

查看:72
本文介绍了KeyError:使用Python Instagram API客户端的“数据"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将此客户端 python-instagram MacOS一起使用

I'm using this client python-instagram with Python 3.4.3 on MacOS.

这是我的步骤:

  • instagram上注册了一个新客户端,收到了client_idclient_secret
  • 管道安装python-instagram
  • sample_app.py复制到我的Mac
  • Registered a new client on instagram, received client_id and client_secret
  • Pip install python-instagram
  • Copy sample_app.py to my mac

我按照 Sample app 上的说明进行操作,我已成功授权我通过instagram应用程序并尝试了示例列表,但没有一个有效.单击我的<h2>标头和API请求计数器后,看到Remaining API Calls = 486/500.

I followed the instructions on Sample app, I successfully authorized my app via instagram and tried this list of examples, but none of them worked. After my click the <h2> header and counter of API requests changes and I see Remaining API Calls = 486/500.

如果我尝试获取User Recent Media,则在终端中显示KeyError: 'data'异常.如果删除try - except构造,则在try中保留块,此时将看到错误:500 Internal Server Error".

If I try to get User Recent Media an exception KeyError: 'data' shows in my terminal. If I delete try - except construction, leaving block in try, when I'll see 'Error: 500 Internal Server Error'.

这是回溯:

Traceback (most recent call last):
File "/Users/user/.envs/insta/lib/python3.4/site-packages/bottle.py", line 862, in _handle
return route.call(**args)
File "/Users/user/.envs/insta/lib/python3.4/site-packages/bottle.py", line 1732, in wrapper
rv = callback(*a, **ka)
File "sample_app.py", line 79, in on_recent
recent_media, next = api.user_recent_media()
File "/Users/user/.envs/insta/lib/python3.4/site-packages/instagram/bind.py", line 197, in _call
return method.execute()
File "/Users/user/.envs/insta/lib/python3.4/site-packages/instagram/bind.py", line 189, in execute
content, next = self._do_api_request(url, method, body, headers)
File "/Users/user/.envs/insta/lib/python3.4/site-packages/instagram/bind.py", line 151, in _do_api_request
obj = self.root_class.object_from_dictionary(entry)
File "/Users/user/.envs/insta/lib/python3.4/site-packages/instagram/models.py", line 99, in object_from_dictionary
for comment in entry['comments']['data']:
KeyError: 'data'

我使用的所有代码均来自Instagram的官方python API客户端示例.

All the code I used is from the sample of the official python API client by Instagram.

推荐答案

有一个开放的 Github issue 针对此错误,已发送 fix ,但尚未合并.

There is an open Github issue for this bug, a fix was sent, but it's not merged yet.

在已安装的程序包中的models.py中添加一行修补程序.

Add the one line fix to models.py on your installed package.

使用sudo打开:

sudo vi /Library/Python/2.7/site-packages/instagram/models.py  # Use relevant python version 

在第99行上,添加以下内容:

On line 99, add this:

if "data" in entry["comments"]:

在接下来的两行正确缩进:

Correct indentation on next two lines:

       for comment in entry['comments']['data']: 
           new_media.comments.append(Comment.object_from_dictionary(comment))

这篇关于KeyError:使用Python Instagram API客户端的“数据"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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