AttributeError: 'module' object has no attribute 'urlopen'
Asked Answered
U

13

194

I'm trying to use Python to download the HTML source code of a website but I'm receiving this error.

Traceback (most recent call last):  
    File "C:\Users\Sergio.Tapia\Documents\NetBeansProjects\DICParser\src\WebDownload.py", line 3, in <module>
     file = urllib.urlopen("http://www.python.org")
AttributeError: 'module' object has no attribute 'urlopen'

I'm following the guide here: http://www.boddie.org.uk/python/HTML.html

import urllib

file = urllib.urlopen("http://www.python.org")
s = file.read()
f.close()

#I'm guessing this would output the html source code?
print(s)

I'm using Python 3.

Ursala answered 19/10, 2010 at 14:59 Comment(0)
H
340

This works in Python 2.x.

For Python 3 look in the docs:

import urllib.request

with urllib.request.urlopen("http://www.python.org") as url:
    s = url.read()
    # I'm guessing this would output the html source code ?
    print(s)
Hagiographa answered 19/10, 2010 at 15:7 Comment(5)
Hi Eumiro, using the 'with' statement in Python I'm guessing it closes the connection automatically once it's done using it? Similar to a use statement in C#?Ursala
@Sergio: exactly! And through the indentation you see where your file is still opened.Hagiographa
Hello @eumiro, I have an error of "IndentationError: expected an indented block" when I type s = url.read(), may I ask how can I solve it please? xProlamine
@KarenChan you're missing an indent before s=url.read(); have you got 4 spaces before it?Champignon
I get HTTPError: HTTP Error 503: Service Unavailable when I use your methodLonglived
M
28

A Python 2+3 compatible solution is:

import sys

if sys.version_info[0] == 3:
    from urllib.request import urlopen
else:
    # Not Python 3 - today, it is most likely to be Python 2
    # But note that this might need an update when Python 4
    # might be around one day
    from urllib import urlopen


# Your code where you can use urlopen
with urlopen("http://www.python.org") as url:
    s = url.read()

print(s)
Mattock answered 7/12, 2015 at 7:11 Comment(1)
with urlopen("http://www.python.org") as url: does not work in python2 with AttributeError: addinfourl instance has no attribute '__exit__'. Need to write url = urlopen("http://www.python.org") Cobber
G
22
import urllib.request as ur
s = ur.urlopen("http://www.google.com")
sl = s.read()
print(sl)

In Python v3 the "urllib.request" is a module by itself, therefore "urllib" cannot be used here.

Geber answered 13/4, 2015 at 17:15 Comment(0)
S
11

To get 'dataX = urllib.urlopen(url).read()' working in python3 (this would have been correct for python2) you must just change 2 little things.

1: The urllib statement itself (add the .request in the middle):

dataX = urllib.request.urlopen(url).read()

2: The import statement preceding it (change from 'import urlib' to:

import urllib.request

And it should work in python3 :)

Scleritis answered 14/6, 2017 at 14:33 Comment(0)
M
9

Change TWO lines:

import urllib.request #line1

#Replace
urllib.urlopen("http://www.python.org")
#To
urllib.request.urlopen("http://www.python.org") #line2

If You got ERROR 403: Forbidden Error exception try this:

siteurl = "http://www.python.org"

req = urllib.request.Request(siteurl, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36'})
pageHTML = urllib.request.urlopen(req).read()

I hope your problem resolved.

Morville answered 22/2, 2020 at 23:56 Comment(0)
M
3
import urllib.request as ur

filehandler = ur.urlopen ('http://www.google.com')
for line in filehandler:
    print(line.strip())
Mainsail answered 27/4, 2015 at 4:43 Comment(0)
U
1

For python 3, try something like this:

import urllib.request
urllib.request.urlretrieve('http://crcv.ucf.edu/THUMOS14/UCF101/UCF101/v_YoYo_g19_c02.avi', "video_name.avi")

It will download the video to the current working directory

I got help from HERE

Unfurl answered 13/7, 2018 at 7:38 Comment(0)
C
1

Solution for python3:

from urllib.request import urlopen

url = 'http://www.python.org'
file = urlopen(url)
html = file.read()
print(html)
Courier answered 17/7, 2018 at 7:46 Comment(1)
Simple and easy to understand for beginners. ThanksOilbird
S
0

If your code uses Python version 2.x, you can do the following:

from urllib.request import urlopen
urlopen(url)

By the way, I suggest another module called requests, which is more friendly to use. You can use pip install it, and use it like this:

import requests
requests.get(url)
requests.post(url)
Selfaddressed answered 20/8, 2018 at 10:22 Comment(0)
E
0
import urllib
import urllib.request
from bs4 import BeautifulSoup


with urllib.request.urlopen("http://www.newegg.com/") as url:
    s = url.read()
    print(s)
soup = BeautifulSoup(s, "html.parser")
all_tag_a = soup.find_all("a", limit=10)

for links in all_tag_a:
    #print(links.get('href'))
    print(links)
Eleanor answered 19/6, 2019 at 15:12 Comment(0)
C
0

One of the possible way to do it:

import urllib
...

try:
    # Python 2
    from urllib2 import urlopen
except ImportError:
    # Python 3
    from urllib.request import urlopen
Caller answered 28/8, 2019 at 16:46 Comment(0)
D
0

Use the third-party six module to make your code compatible between Python2 and Python3.

from six.moves import urllib
urllib.request.urlopen("<your-url>")
Denney answered 19/5, 2020 at 16:24 Comment(1)
You can import six module in this way from six.moves import urllibDenney
T
-3
imgResp = urllib3.request.RequestMethods.urlopen(url)

Add this RequestMethods before using urlopen

Twentyone answered 6/5, 2021 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.