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

3 投票
1 回答
3056 浏览
提问于 2025-04-29 14:38

我正在尝试登录,并从接下来的页面获取网址:

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()

...到现在为止一切顺利。现在我需要从重定向后的页面获取网址,有什么帮助吗?

暂无标签

1 个回答

4

br.geturl() 应该可以解决这个问题。可以使用 httpbin.org 的重定向接口来进行测试:

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

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

撰写回答