scraper tutorial error message

A forum for general discussion of the Python programming language.

scraper tutorial error message

Postby AEA » Fri May 17, 2013 2:03 am

I am following a tutorial for web scraping, and I have been stopped very early on, I am using version 3.3. I am trying to run the code:

Code: Select all
import urllib

htmlfile = urllib.urlopen("http://google.com")

htmltext = htmlfile.read()

print (htmltext)


and it is returning the error message :
Code: Select all
Traceback (most recent call last):
  File "C:/Program Files (x86)/Notepad++/TestScraper1.py", line 3, in <module>
    htmlfile = urllib.urlopen("http://google.com")
AttributeError: 'module' object has no attribute 'urlopen'
>>>


Any ideas? I am sure this is a stupid question, but I am a complete novice.

Many thanks
AEA
 
Posts: 32
Joined: Thu Apr 18, 2013 11:37 am

Re: scraper tutorial error message

Postby metulburr » Fri May 17, 2013 2:22 am

its urllib.request.urlopen() in python3.x. And just for some extra info, the same in python2.x is urllib2.urlopen().
You have to be careful when using tutorials as most are written in 2.x and using 3.x modules have moved or renamed.
Code: Select all
import urllib.request

htmlfile = urllib.request.urlopen("http://google.com")

htmltext = htmlfile.read().decode()

print (htmltext)
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1471
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 4 guests