如何在 python 3.x 中检索和显示 Vimeo 视频的 JSON 数据?
我想在给定视频 URL 的情况下在 python 3.2 中检索和使用基本 Vimeo 数据。我是 JSON(和 python)的新手,但它看起来很适合这样做。
- 请求 Vimeo 视频数据(通过 API 格式的 .json URL)
- 将返回的 JSON 数据转换为 python 字典
- 显示字典键和字符串数据(“id”,“标题”,“描述”等)
另一个SO页面通过 url 获取 json 数据并在 python 中使用 在 python 2.x 中做了类似的事情,但是语法的变化(比如集成 urllib2)让我尝试了这个。
>>> import urllib
>>> import json
>>> req = urllib.request.urlopen("http://vimeo.com/api/v2/video/31161781.json")
>>> opener = urllib.request.build_opener()
>>> f = opener.open(req)
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
f = opener.open(req)
File "C:\Python32\lib\urllib\request.py", line 358, in open
protocol = req.type
AttributeError: 'HTTPResponse' object has no attribute 'type'
该代码将集成到现有项目中,因此我必须使用 python。我对 HTTP 查询有足够的了解,可以猜测该响应对象中的数据,但对 python 的了解还不够,无法理解打开失败的原因以及如何正确引用它。我应该尝试什么来代替 opener.open(req)
?
I want to retrieve and work with basic Vimeo data in python 3.2, given a video's URL. I'm a newcomer to JSON (and python), but it looked like the right fit for doing this.
- Request Vimeo video data (via an API-formatted .json URL)
- Convert returned JSON data into python dict
- Display dict keys & data ("id", "title", "description", etc.)
Another SO page Get json data via url and use in python did something similar in python 2.x, but syntax changes (like integrating urllib2) led me to try this.
>>> import urllib
>>> import json
>>> req = urllib.request.urlopen("http://vimeo.com/api/v2/video/31161781.json")
>>> opener = urllib.request.build_opener()
>>> f = opener.open(req)
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
f = opener.open(req)
File "C:\Python32\lib\urllib\request.py", line 358, in open
protocol = req.type
AttributeError: 'HTTPResponse' object has no attribute 'type'
This code will integrate into an existing project, so I'm tied to using python. I know enough about HTTP queries to guess the data's within that response object, but not enough about python to understand why the open failed and how to reference it correctly. What should I try instead of opener.open(req)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这对我有用:
或者有请求:
This works for me:
Or with Requests:
你可以尝试像这样请求url吗?
正如你所看到的,Python有很多共享功能的库,你不需要构建一个开启器或任何东西来获取这些数据。
Can you try to just request the url like so
As you see python has a lot of libraries that share functionality, you shouldn't need to build an opener or anything to get this data.
查看:http://www.voidspace.org.uk/python/articles/ urllib2.shtml
Check out: http://www.voidspace.org.uk/python/articles/urllib2.shtml
你可以尝试这样:
you can try to like so: