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.
When I run this part of the code then things doesn't seem right, what am I doing wrong?
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.string artist = col.string album = col.string cover_link = col.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()