如何从Python中的Mechanize获取当前URL?

时间:2014-10-25 10:26:46

标签: python web login mechanize

我尝试登录并从即将到来的网页获取网址:

br = mechanize.Browser()
url = "http://www.blahblah.com/login-page/"
br.open( url )

br.select_form(nr = 1)
br.form['username'] = "Foo"
br.form['password'] = "fooPswRd"
br.submit()

......到目前为止一切顺利。现在我需要一个来自重定向页面的URL,任何帮助吗?

1 个答案:

答案 0 :(得分:4)

br.geturl()应该这样做。使用httpbin.org's重定向端点进行测试:

br = mechanize.Browser()
url = 'http://httpbin.org/redirect-to?url=http%3A%2F%2Fstackoverflow.com'
br.open( url )

>>> print br.geturl()
http://stackoverflow.com