如何在python 3.x中检索和显示Vimeo视频的JSON数据?

时间:2012-01-20 21:24:02

标签: python json vimeo

我希望在给定视频的URL的情况下检索并使用python 3.2中的基本Vimeo数据。我是JSON(和python)的新手,但它看起来非常适合这样做。

  1. 请求Vimeo视频数据(通过API格式的.json网址)
  2. 将返回的JSON数据转换为python dict
  3. 显示字典键和&数据(“id”,“title”,“description”等)
  4. 另一个SO页面Get json data via url and use in 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不足以理解为什么open失败以及如何正确引用它。我该怎么做而不是opener.open(req)

4 个答案:

答案 0 :(得分:8)

这对我有用:

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'))

或使用请求:

import requests

data = requests.get('http://vimeo.com/api/v2/video/31161781.json').json()

答案 1 :(得分: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'>

答案 2 :(得分:1)

你可以试着像这样请求网址吗

response = urllib.urlopen('http://www.weather.com/weather/today/Ellicott+City+MD+21042')
response_dict = json.loads(response.read())

如你所见,python有很多共享功能的库,你不需要构建一个开放器或任何东西来获取这些数据。

答案 3 :(得分:0)

您可以尝试这样:

import requests
url1 = 'http://vimeo.com/api/v2/video/31161781.json'
html = requests.get(url1)
html.encoding = html.apparent_encoding
print(html.text)