# problem with python program



## doughy (Apr 3, 2017)

I have the following code and can not figure out why it won't run.  I've tried it on Linux and FreeBSD with the same results:

```
import json
from urllib import urlopen

url = "https://gdata.youtube.com/feeds/api/standardfeeds/top_rated?alt=json"
response = urlopen(url)
contents = response.read()
text = contents.decode('utf8')
data = json.loads(text)

for video in data['feed']['entry'][0:6]:
    print(video['title']['$t'])
```
This is the output I get when I run this code.

```
Traceback (most recent call last):
  File "youtube.py", line 8, in <module>
    data = json.loads(text)
  File "/usr/local/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
```
I was careful about syntax and really don't know why this won't run.


----------



## tobik@ (Apr 3, 2017)

https://gdata.youtube.com/feeds/api/standardfeeds/top_rated?alt=json does not exist. Try opening it in your browser to see what I mean.


----------



## Beastie (Apr 3, 2017)

Implement exceptions for your code or at least check outputs for valid data. You'll avoid headaches when your program becomes thousands of SLOCs big.


----------



## SirDice (Apr 3, 2017)

Beastie said:


> Implement exceptions for your code or at least check outputs for valid data.


Indeed. Never assume your request for a resource succeeds. Always assume things can and will fail.


----------



## doughy (Apr 3, 2017)

thanks


----------

