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()
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With