如何在Python 3.x中获取和显示Vimeo视频的JSON数据?
我想在Python 3.2中获取并处理一些基本的Vimeo视频数据,给定一个视频的URL。我是JSON(和Python)的新手,但我觉得这应该是实现这个目标的好方法。
- 请求Vimeo视频数据(通过一个API格式的.json URL)
- 把返回的JSON数据转换成Python字典
- 显示字典中的键和值(比如“id”、“title”、“description”等)
另一个StackOverflow页面 获取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)
?
4 个回答
1
你可以试着像这样请求网址:
response = urllib.urlopen('http://www.weather.com/weather/today/Ellicott+City+MD+21042')
response_dict = json.loads(response.read())
正如你所看到的,Python有很多库可以实现相似的功能,所以你不需要自己去写一个打开器或者其他复杂的东西来获取这些数据。
1
可以看看这个链接: http://www.voidspace.org.uk/python/articles/urllib2.shtml
>>> import urllib2
>>> import json
>>> req = urllib2.Request("http://vimeo.com/api/v2/video/31161781.json")
>>> response = urllib2.urlopen(req)
>>> content_string = response.read()
>>> content_string
'[{"id":31161781,"title":"Kevin Fanning talks about hiring for Boston startups","description":"CogoLabs.com talent developer and author Kevin Fanning talks about hiring for small teams in Boston, how job seekers can make themselves more attractive, and why recruiters should go the extra mile to attract talent.","url":"http:\\/\\/vimeo.com\\/31161781","upload_date":"2011-10-26 15:37:35","thumbnail_small":"http:\\/\\/b.vimeocdn.com\\/ts\\/209\\/777\\/209777866_100.jpg","thumbnail_medium":"http:\\/\\/b.vimeocdn.com\\/ts\\/209\\/777\\/209777866_200.jpg","thumbnail_large":"http:\\/\\/b.vimeocdn.com\\/ts\\/209\\/777\\/209777866_640.jpg","user_name":"Venture Cafe","user_url":"http:\\/\\/vimeo.com\\/venturecafe","user_portrait_small":"http:\\/\\/b.vimeocdn.com\\/ps\\/605\\/605070_30.jpg","user_portrait_medium":"http:\\/\\/b.vimeocdn.com\\/ps\\/605\\/605070_75.jpg","user_portrait_large":"http:\\/\\/b.vimeocdn.com\\/ps\\/605\\/605070_100.jpg","user_portrait_huge":"http:\\/\\/b.vimeocdn.com\\/ps\\/605\\/605070_300.jpg","stats_number_of_likes":0,"stats_number_of_plays":43,"stats_number_of_comments":0,"duration":531,"width":640,"height":360,"tags":"startup stories, entrepreneurship, interview, Venture Cafe, jobs","embed_privacy":"anywhere"}]'
>>> loaded_content = json.loads(content_string)
>>> type(content_string)
<type 'str'>
>>> type(loaded_content)
<type 'list'>
9
这个对我有效:
import urllib.request, json
response = urllib.request.urlopen('http://vimeo.com/api/v2/video/31161781.json')
content = response.read()
data = json.loads(content.decode('utf8'))
或者用Requests库:
import requests
data = requests.get('http://vimeo.com/api/v2/video/31161781.json').json()