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

发布于 2024-12-28 15:03:32 字数 1088 浏览 6 评论 0原文

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

  1. 请求 Vimeo 视频数据(通过 API 格式的 .json URL)
  2. 将返回的 JSON 数据转换为 python 字典
  3. 显示字典键和字符串数据(“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.

  1. Request Vimeo video data (via an API-formatted .json URL)
  2. Convert returned JSON data into python dict
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暮倦 2025-01-04 15:03:32

这对我有用:

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

This works for me:

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

Or with Requests:

import requests

data = requests.get('http://vimeo.com/api/v2/video/31161781.json').json()
若能看破又如何 2025-01-04 15:03:32

你可以尝试像这样请求url吗?

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

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

Can you try to just request the url like so

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

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.

不再让梦枯萎 2025-01-04 15:03:32

查看: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'>

Check out: 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'>
旧时模样 2025-01-04 15:03:32

你可以尝试这样:

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

you can try to like so:

import requests
url1 = 'http://vimeo.com/api/v2/video/31161781.json'
html = requests.get(url1)
html.encoding = html.apparent_encoding
print(html.text)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文