python urllib usage

15,394

Solution 1

You are confusing python3 package urllib.request with Python2.7 one which is urllib2. Please don't do that. Python3 and Python2 are libraries are different. All you may want is urllib2 from python2

import urllib2
from urllib2 import Request
req = Request("yoururl")
res = urllib2.urlopen(req)

Solution 2

The urllib package is just that, a package. It's __init__.py does not import urllib.request and thus you cannot simply reach urllib.request by only importing urllib. It is intended as a namespace only.

Import urllib.request instead.

Solution 3

Both import X and from X import Y perform an import of whatever module or package X that is given.

In this case, urllib is a package. When you import urllib, then the package itself is imported, and you get a reference to it, but any submodules are not imported (in this case). When you do from urllib.request import ..., Python actually imports the entire module urllib.request, but then picks out the names that you asked for and gives you references to them.

If you aren't using urlopen, then you could also easily do import urllib.request and get the same result.

Share:
15,394
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I imported two libraries urllib and from urllib.request import urlopen.

    The second one is contained in the first

    When I went over the code and tried to remove the from urllib.request import urlopen line , I got this message:

    opnerHTMLnum = urllib.request.build_opener()
    AttributeError: 'module' object has no attribute 'request'
    

    When I restore the from urllib.request import urlopen line the code runs .

    Can anyone explain why?

    import re
    #import http.cookiejar
    import os.path
    #import time
    #import urllib3
    import urllib
    from urllib.request import urlopen
    import sys
    import smtplib
    from email.mime.text import MIMEText
    
    # ...
    
        opnerHTMLnum = urllib.request.build_opener()
    
  • Martijn Pieters
    Martijn Pieters over 11 years
    I think the OP got confused with the tags; if he really was on Python 2.7, there would have been an import error, not the behaviour stated in the question.
  • markc
    markc almost 7 years
    Good catch, I was following this example: nltk.org/book/ch03.html and I didn't realise the examples referred to python3. My env is Python 2.7. +1 voted, thanks!