Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python mechanize to follow image links?

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"><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.

like image 612
evgeniuz Avatar asked Dec 20 '25 08:12

evgeniuz


1 Answers

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>
br.back()
br.follow_link(text='[IMG]', nr=1)
>>><Browser visiting http://www.systempuntoout.com/about>
like image 196
systempuntoout Avatar answered Dec 22 '25 21:12

systempuntoout