Question

I just starting looking at the Python version of Mechanize today. I took most of this code from the first example on http://wwwsearch.sourceforge.net/mechanize/. The documentation of this module is very sparse and I have no idea how to debug this.

I am trying to find and follow the first link with the text "Careers". When I run this I get this error "mechanize._mechanize.LinkNotFoundError". Can anyone tell me what I am doing wrong?

import re
import mechanize

br = mechanize.Browser(factory=mechanize.RobustFactory())
br.open("http://www.amazon.com/")

response1 = br.follow_link(text_regex=r"Careers", nr=1)
assert br.viewing_html()
print br.title()
Was it helpful?

Solution

I just tried the sample code myself, and it looks like the problem is with the nr argument. It's not documented anywhere but in the source code (which is far more informative than the documentation!), and it states that:

nr: matches the nth link that matches all other criteria (default 0)

Because the nr argument is 0-based, when you gave the argument of 1, it was looking for the second mention of Careers, which was obviously nothing.

Because it defaults to 0, or the first link found, you can set the nr argument to 0, or leave it off entirely.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top