mechanize._mechanize.LinkNotFoundError

2019-09-07 19:40发布

I try to imitate a link click using this script:

#!/usr/bin/env python

import mechanize

targetPage = 'http://example.com/'
clickUrl="http://someurlinsideexample.com/" 

br = mechanize.Browser(factory=mechanize.RobustFactory())
br.open(targetUrl)
br.follow_link(url=clickUrl)

but I get this error:

  File "/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg/mechanize/_mechanize.py", line 620, in find_link
    raise LinkNotFoundError()
mechanize._mechanize.LinkNotFoundError

What's wrong with my snippet and how to fix it?

1条回答
forever°为你锁心
2楼-- · 2019-09-07 20:02

Probably need a bit more info to help you better, but have you tried

for link in br.links():
    if link.url == clickUrl:
        br.follow_link(link)

or perhaps

br.follow_link(text='theactualtextinthelink')

of course the above wont work for images as far as i know

查看更多
登录 后发表回答