I'm trying to open the URL of this API from the sunlight foundation and return the data from the page in JSON. This is the code I've produced, minus the parenthesis around my API key.
import urllib.request.urlopen
import json
urllib.request.urlopen("https://sunlightlabs.github.io/congress/legislators?api_key='(myapikey)")
And I'm getting this error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
ImportError: No module named request.urlopen
What am I doing wrong? I've looked into https://docs.python.org/3/library/urllib.request.html and still no progress.
You need to use from urllib.request import urlopen
, also I suggest you use the with
statement while opening a connection.
from urllib.request import urlopen
with urlopen("https://sunlightlabs.github.io/congress/legislators?api_key='(myapikey)") as conn:
# dosomething