evgeniuz
evgeniuz

Reputation: 2769

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.

Upvotes: 3

Views: 2900

Answers (1)

systempuntoout
systempuntoout

Reputation: 74104

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>

Upvotes: 5

Related Questions