How to get links on a webpage using mechanize and open those links

mechanize, python

Solution

Here is an example from the project's page:

import re
from mechanize import Browser

br = Browser()
br.open("http://www.example.com/")

# ...

# .links() optionally accepts the keyword args of .follow_/.find_link()
for link in br.links(url_regex="python.org"):
    print link
    br.follow_link(link)  # takes EITHER Link instance OR keyword args
    br.back()

Problem

I want to use mechanize with python to get all the links of the page, and then open the links.How can I do it?

Original source