Well, I don't know how to do it using Mechanize, however I know how to do in using lxml
:
Lets assume that our webpage has this code:
<a href="page2.html"><img name="bla bla" id="next" src="Cat.jpg"></a>
. Using lxml
we would write this code:
from lxml import html
page = urlllib2.urlopen('http://example.com')
tree = html.fromstring(page.read())
link = tree.xpath('//img[@id="next"]/ancestor::a/attribute::href')
Most of the magic happens in the tree.xpath
function, where you define the image you're looking for first with //img[@id="next"]
, then you specify that you're looking for the a
tag right before it: /ancestor::a
and that you're looking for specifically the href
attribute: /attribute::href
. The link variable will now contain a list of strings matching that query - in this case link[0] will be page2.html
- which you can urlopen()
, thus effectively clicking it.
For the //img[@id="next"]
part, you can use other attribute, for example this: //img[@name="bla bla"]
and it's going to work perfectly fine. You just need to think which attribute is better for this situation.
I know this answer doesn't use Mechanize, however I hope it's a helpful pointer. Good luck!