Search code examples
python-2.7mechanize-python

How to find the current URL in python mechanize?


I see the answer to how to get the current URL in mechanize for Ruby (mechanize how to get current url) but how would one do so for the python mechanize?

The documentation doesn't seem very...thorough...


Solution

  • Well, it may not be very thorough, but still there is what you need:

    import mechanize
    
    br = mechanize.Browser()
    br.open("http://www.example.com/")
    # follow second link with element text matching regular expression
    response1 = br.follow_link(text_regex=r"cheese\s*shop", nr=1)
    
    print response1.geturl()
    

    As a side-note, when I'm looking for method like that and I don't find them in the docs, I usually open an IPython shell and I play with the autocompletion to see if there is some method that seems nice.