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

问题描述 投票:1回答:4

给定视频的URL,我想在python 3.2中检索并使用基本的Vimeo数据。我是JSON(和python)的新手,但看起来很适合执行此操作。

  1. 请求Vimeo视频数据(通过API格式的.json URL)
  2. 将返回的JSON数据转换为python字典
  3. 显示字典键和数据(“ id”,“ title”,“ description”等)

另一个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却了解的不够,因此无法理解为什么打开失败以及如何正确引用它。我应该怎么做才能代替opener.open(req)

python json vimeo
4个回答
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
投票

您可以尝试仅这样请求网址

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

0
投票

您可以尝试这样:

import requests
url1 = 'http://vimeo.com/api/v2/video/31161781.json'
html = requests.get(url1)
html.encoding = html.apparent_encoding
print(html.text)
© www.soinside.com 2019 - 2024. All rights reserved.