开发者

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开发者_如何学C="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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜