编程语言
首页 > 编程语言> > 如何在Python中从Mechanize获取当前URL?

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

作者:互联网

我正在尝试登录并从下一页获取URL:

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,有什么帮助吗?

解决方法:

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()
https://stackoverflow.com

标签:web,login,mechanize,python
来源: https://codeday.me/bug/20191029/1956816.html