2012-01-20 4 views
1

私は、ビデオのURLが与えられていれば、python 3.2で基本的なVimeoデータを取得して使いたいと思っています。私はJSON(およびPython)の初心者ですが、これを行うのに適しています。VimeoビデオのJSONデータをPython 3.xで取得して表示する方法は?

  1. 要求Vimeoの(API形式の.jsonのURLを経由して)ビデオデータ
  2. 変換はPythonの辞書にJSONデータを返さ
  3. 表示dictのキー&データ( "ID"、 "タイトル"、 "説明"など)

もう1つの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)の代わりに何を試してください。

答えて

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

最初に働いた、ありがとう! –

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

おかげで、あなたの例では、私が探しているデータを返しました。残念ながら、python 3.xはurllib2をurllibに移動し、要求とurlopenに異なる構文を使用します。私はurllib.urlopen()をurllib.request.urlopen()に変更しましたが、それは私の質問に 'Response = urllib.request.urlopen(req)'という文で示されたものと同じAttributeErrorを生成します。任意のアイデアなぜPython 3のバージョンはこれを見ている? –

1

は、あなただけのようなURLを要求しようとすることができますあなたはPythonが機能を共有ライブラリの多くを持って見るように

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

、あなたは必要はありませんこのデータを取得するためのオープナーなどを構築します。

+0

ありがとう、私はこれが簡単な解決策でなければならないことに同意します。 urllib.urlopen()文でエラーが発生したので、urllib.request.urlopen()に変更してHTTPResponseオブジェクトを取得しました。 Response.read()は "TypeError:バイト様オブジェクトに文字列パターンを使用できません"を生成するようになりました。別のコンバージョンステップがありませんか? –

関連する問題