<p><code>br.follow_link</code>接受<code>Link</code>对象或关键字arg(例如<code>nr=0</code>)。</p>
<p><code>br.links()</code>列出所有链接。</p>
<p><code>br.links(url_regex='...')</code>列出其url与regex匹配的所有链接。</p>
<p><code>br.links(text_regex='...')</code>列出其链接文本与regex匹配的所有链接。</p>
<p><code>br.follow_link(nr=num)</code>跟随页面上的第<code>num</code>个链接,计数从0开始。它返回一个响应对象(与br.open(…)返回的类型相同)</p>
<p><code>br.find_link(url='...')</code>返回其<code>url</code>正好等于给定url的<code>Link</code>对象。</p>
<p><code>br.find_link</code>、<code>br.links</code>、<code>br.follow_link</code>、<code>br.click_link</code>都接受相同的关键字。运行<code>help(br.find_link)</code>查看有关这些关键字的文档。</p>
<p><strong>编辑:</strong>如果您有一个要跟踪的目标url,可以执行以下操作:</p>
<pre><code>import mechanize
br = mechanize.Browser()
response=br.open("http://www.example.com/")
target_url='http://www.rfc-editor.org/rfc/rfc2606.txt'
for link in br.links():
print(link)
# Link(base_url='http://www.example.com/', url='http://www.rfc-editor.org/rfc/rfc2606.txt', text='RFC 2606', tag='a', attrs=[('href', 'http://www.rfc-editor.org/rfc/rfc2606.txt')])
print(link.url)
# http://www.rfc-editor.org/rfc/rfc2606.txt
if link.url == target_url:
print('match found')
# match found
break
br.follow_link(link) # link still holds the last value it had in the loop
print(br.geturl())
# http://www.rfc-editor.org/rfc/rfc2606.txt
</code></pre>