Network is unreachable


#1

My code is as follows:

from urllib2 import urlopen
from json import load

apiUrl = "http://www.nhtsa.gov/webapi/api/SafetyRatings"
apiParam = ""
outputFormat = "?format=json"

#Combine all three variables to make up the complete request URL
response = urlopen(apiUrl + apiParam + outputFormat)

#code below is only to handle JSON response object/format
#use equivalent sets of commands to handle xml response object/format
json_obj = load(response)

#Load the Result (vehicle collection) from the JSON response
print '------------------------------'
print '           Result			 '
print '------------------------------'
for objectCollection in json_obj['Results']:
	# Loop each vehicle in the vehicles collection
    for safetyRatingAttribute, safetyRatingValue in objectCollection.iteritems():
        print safetyRatingAttribute, ": ", safetyRatingValue

# After running this example, feel free to explore the results below

When I try to run the code, I get this result:

Traceback (most recent call last):
File "python", line 9, in <module>
URLError: <urlopen error [Errno 101] Network is unreachable>

Is this API lesson able to be completed since the network is unreachable?


#2

Your code works for me. However, I am getting a similar error for #5....


#3

The error disappeared after the maintenance today.


#4

Thanks, have you tried lesson 5? It is not working for me. I do a print(response) to try to see what is going on and I get something like:

>

and this error:

Traceback (most recent call last):
File "python", line 20, in
ValueError: No JSON object could be decoded


#5

Paste your code and I'll compare it to mine, because mine worked.
Also, you can highlight your code and hit ctrl+k to make it automatically indented


#7
#import first library
from urllib2 import urlopen
#import second library
from json import load




#Declare and assign value for apiUrl variable here
apiURL="http://www.nhtsa.gov/webapi/api/SafetyRatings"
#Declare and assign value for apiParam variable here 
apiParam="/modelyear/2004/make/lexus/model/gx470/vehicleid/1191"   
#Declare and assign value for outputFormat variable for response format in querystring
outputFormat="?format=json" 

#Combine all three variables to make up the complete request URL 
response = urlopen(apiURL + apiParam + outputFormat) 

#code below is only to handle JSON response object/format
#use equivalent sets of commands to handle xml response object/format
print(response)
json_obj = load(response)


#Load the Result (vehicle collection) from the JSON response  
print '------------------------------'
print '           Result			 '
print '------------------------------'
for objectCollection in json_obj['Results']:
	# Loop each vehicle in the vehicles collection  
    for safetyRatingAttribute, safetyRatingValue in objectCollection.iteritems():
        print safetyRatingAttribute, ": ", safetyRatingValue   

# After running this example, feel free to explore the results below

#8

If you look at the hint, it says:

Only VehicleId parameter (remove the modelyear, make and model name/value pairs from apiParam variable)
apiParam = "/VehicleId/7045"

So in your apiParam, you should only have "/VehicleId/1191"

and for mine, before json_obj = load(response), I don't have print(response). I don't that should be impacting the JSON, though.


#9

Thanks! I should've noticed that. But...now I get the Network is unreachable error.

By the way...I was trying to get this code to work in Python 3.4...seems like you need to use urllib.request instead of urllib2.


#10

Try clearing your internet browser cache, and all that good stuff then try again. I got the network error earlier as well, but it's working now. Perhaps they use Python 2 instead of 3 here?
edit:
according to this link: http://help.codecademy.com/customer/en/portal/articles/1403551-what-version-of-python-do-you-teach-
They teach Python 2.7.3 on here, so that's the reason why your 3.4 code wasn't working.


#11

Yes, I believe so. I just started Python recently, so I am no expert, but I can tell you raw_input() is Python 2....

Tried clearing the browser, but same result. I will try again later.

Any thoughts as to what you will do after Python. Other languages? More Python somewhere? Just curious.


#12

I was going to use some other tutorial sites along with some programming prompts:
http://pygame.org/news.html
http://www.tutorialspoint.com/python/index.htm
https://www.reddit.com/r/DailyProgrammer
https://www.reddit.com/r/ProgrammingPrompts

Unfortunately there aren't very many free sites like codecademy that go further in depth.

Will also be learning Ruby after this.


#13

Cool (and thanks for the links). I am planning to try to do more with Pygame. I was able to successfully install the module with the instructions here:

http://programarcadegames.com/index.php?lang=en

Installing it is difficult!

I was thinking to do the course here https://www.coursera.org/course/interactivepython1 but it is not starting until January...


#14

Here's a few more links for you:
http://codingbat.com
http://learnpython.org
http://www.programmr.com/practice/


#15

Yea, coursera is pretty cool since it's like an actual class environment. Keep sticking with it, I'm sure you'll get everything figured out.


#16

Appreciate it, thanks again.

Any thoughts as to what you would like to do with Ruby? (I don't know too much about it..)