opening a url with urllib in python 3

Question:

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.

Answers:

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
Answered By: styvane

In Python 3 You can implement that this way:

import urllib.request
u = urllib.request.urlopen("xxxx")#The url you want to open

Pay attention:
Some IDE can import urllib(Spyder) directly, while some need to import urllib.request(PyCharm).

That’s because you sometimes need to explicitly import the pieces you want, so the module doesn’t need to load everything up when you just want a small part of it.

Hope this will help.

Answered By: Lyn

urllib.request is a module whereas urlopen is a function.
check out this link, it can help you clear your doubts.
https://docs.python.org/3.0/library/urllib.request.html

Answered By: Fazil Ahmed
from urllib.request import urlopen
from bs4 import BeautifulSoup

wiki = "https://en.wikipedia.org/wiki/List_of_state_and_union_territory_capitals_in_India"

page = urlopen(wiki)
soup =  BeautifulSoup(page, "html.parser" ).encode('UTF-8')

print (soup)
Answered By: Curious Flower

The problem is with your module import.
There are multiple ways to do that in Python3.x:

from urllib.request import urlopen
urlopen('example.com/***')

or

from urllib import request
request.urlopen('example.com/***')

or

import urllib
urllib.request.urlopen('example.com/***')

Moreover, you can name the module you want to import as well:

import urllib.request as req  ## or whatever you want
req.urlopen('example.com/***')

You can use the one you prefer, but watch the sequence of modules and packages you are using.

Answered By: Ehsan Tabatabaei
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.