4 Replies - 483 Views - Last Post: 20 April 2013 - 07:29 AM Rate Topic: -----

#1 Orochimaru  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 26-December 12

Don't understand how to correctly work with exceptions.

Posted 20 April 2013 - 05:47 AM

Hi,

I don't understand how to correctly work with exceptions. I tried to read these documents but the amount of information just overloads my brain and confuses me even more.

http://www.tutorials..._exceptions.htm
http://docs.python.o...ial/errors.html

When I run this part of the code then things doesn't seem right, what am I doing wrong?

Code snippet.
try:
    page3 = mech.follow_link(text_regex="Next")
#except:
except LinkNotFoundError:
    print "\nNo more pagination links found.\n"
else:
    print "\n\t --- Link found! --- \n"   


Code snippet - DISPLAY
Traceback (most recent call last):
  File "auto_pagination.py", line 38, in <module>
    except LinkNotFoundError:
NameError: name 'LinkNotFoundError' is not defined



The entire source code.
from mechanize import Browser
from bs4 import BeautifulSoup
import urllib, os

def extract(soup, year):
    table = soup.find("table", border=1)
    for row in table.findAll('tr')[1:]:
        col = row.findAll('td')
        rank = col[0].string
        artist = col[1].string
        album = col[2].string
        cover_link = col[3].img['src']
        record = (str(year), rank, artist, album, cover_link)
        print >> outfile, "|".join(record)
#        save_as = os.path.join("./images/", album + ".jpg")
#        urllib.urlretrieve("http://www.palewire.com" + cover_link, save_as)
#        print "Downloaded %s album cover" % album
        print "Row row, row your boat..."

outfile = open("albums.txt", "w")

mech = Browser()
url = "http://www.palewire.com/scrape/albums/2007.html"
page1 = mech.open(url)
html1 = page1.read()
soup1 = BeautifulSoup(html1)
#print soup.prettify()
#extract(soup1, 2007)

page2 = mech.follow_link(text_regex="Next")
html2 = page2.read()
soup2 = BeautifulSoup(html2)
extract(soup2, 2006)

try:
    page3 = mech.follow_link(text_regex="Next")
#except:
except LinkNotFoundError:
    print "\nNo more pagination links found.\n"
else:
    print "\n\t --- Link found! --- \n"   

outfile.close()




Is This A Good Question/Topic? 0
  • +

Replies To: Don't understand how to correctly work with exceptions.

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3741
  • View blog
  • Posts: 13,086
  • Joined: 12-December 12

Re: Don't understand how to correctly work with exceptions.

Posted 20 April 2013 - 06:08 AM

There is no built-in Exception named LinkNotFoundError. If you encountered this in an example then it should be defined as a user-defined Exception (a custom Exception) somewhere else in the page.

If you wanted to catch any exception then you would just use

except:
    # catches any/all exceptions
    pass

I suggest that you re-read that tutorialspoint link, it looks quite good. Type and run all the examples on that page.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3741
  • View blog
  • Posts: 13,086
  • Joined: 12-December 12

Re: Don't understand how to correctly work with exceptions.

Posted 20 April 2013 - 06:19 AM

If you are not sure of the exact name of the Exception that you wish to watch out for then:

Delete your try..except block temporarily;
Let the Exception occur;
Read the name of the Exception in the error-dialog that occurs.

If this doesn't work for some (strange) reason then you could instead copy the precise text of the error message, and use:

except "The error message text":
    pass

This post has been edited by andrewsw: 20 April 2013 - 06:21 AM

Was This Post Helpful? 1
  • +
  • -

#4 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,307
  • Joined: 21-June 11

Re: Don't understand how to correctly work with exceptions.

Posted 20 April 2013 - 06:23 AM

LinkNotFoundError is an exception defined in the mechanize module. If you want to refer to it, you need to either import it directly just like you did with Browser or import the module and refer to the exception as mechanize.LinkNotFoundError.
Was This Post Helpful? 2
  • +
  • -

#5 Orochimaru  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 26-December 12

Re: Don't understand how to correctly work with exceptions.

Posted 20 April 2013 - 07:29 AM

Hi, ok sepp it works now. And I think I understand what to do in the future. When I face multiple exceptions and how to sort them out.

from mechanize import Browser
import mechanize
from bs4 import BeautifulSoup
import urllib, os


try:
    page3 = mech.follow_link(text_regex="Next")
except mechanize.LinkNotFoundError:
    print "\nNo more pagination links found.\n"
else:
    print "\n\t --- Link found! --- \n"   



[SOLVED]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1