1

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

Community
  • 1
  • 1
Matthew Glidden
  • 382
  • 2
  • 6
  • 15

4 Answers4

9

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()
Blender
  • 289,723
  • 53
  • 439
  • 496
1

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'>
sgallen
  • 2,079
  • 13
  • 10
  • 1
    Thanks, your example returned the data I'm looking for. Unfortunately, python 3.x moved urllib2 into urllib and uses different syntax for requests and urlopen. I changed urllib2.urlopen() to urllib.request.urlopen(), but that generates the same AttributeError shown in my question on the statement `response = urllib.request.urlopen(req)`. Any ideas why the python 3 version is seeing this? – Matthew Glidden Jan 20 '12 at 22:28
1

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.

dm03514
  • 54,664
  • 18
  • 108
  • 145
  • Thanks, I agree this should be a simple solution. The urllib.urlopen() statement gave an error, so I changed it to urllib.request.urlopen(), which retrieves an HTTPResponse object. Response.read() now generates "TypeError: can't use a string pattern on a bytes-like object." Am I missing another conversion step? – Matthew Glidden Jan 20 '12 at 21:45
0

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)