2012-01-20 41 views
1

我想檢索和使用python 3.2中的基本Vimeo數據,給出視頻的URL。我是JSON(和python)的新手,但它看起來像是做這件事的合適人選。如何在python 3.x中檢索和顯示Vimeo視頻的JSON數據?

  1. 請求Vimeo的視頻數據(經由API格式上傳.json URL)
  2. 轉換返回的JSON數據轉換成蟒字典
  3. 顯示字典鍵&數據( 「ID」, 「標題」, 「描述」等)

另一個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使用不同的語法。我將urllib2.urlopen()更改爲urllib.request.urlopen(),但是在語句'response = urllib.request.urlopen(req)'的問題中生成了相同的AttributeError。任何想法爲什麼python 3版本看到這個? –

1

你可以嘗試正如你看到蟒蛇有很多共享功能庫,只是要求像這樣

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:不能在類似字節的對象上使用字符串模式」。我是否錯過了另一個轉換步驟? –

相關問題