views:

370

answers:

1

mechanize's Browser class is great and it's follow_link() function is great too. But what to do with this kind of links:

<a href="http://example.com"&gt;&lt;img src="…"></a>

Is there any way to follow such links? The text attribute of this type of links is simply '[IMG]', so AFAIK, there is no way to differentiate such links. Any help would be appreciated.

+1  A: 

To follow such links you need to add nr parameter to follow_link() method.
Try this:

import mechanize
br = mechanize.Browser()
br.open('http://www.systempuntoout.com')
for link in br.links():
    print(link)
br.follow_link(text='[IMG]', nr=0)
print br
>>><Browser visiting http://www.systempuntoout.com/quiz&gt;
br.back()
br.follow_link(text='[IMG]', nr=1)
>>><Browser visiting http://www.systempuntoout.com/about&gt;
systempuntoout