简体   繁体   English

Python:机械化

[英]Python: Mechanize

My problem i very short and sweet that I'm using mechanize module to sign into a website, but how to find out that a user has successfully signed in or not. 我的问题非常简短,很甜蜜,我正在使用机械化模块登录网站,但是如何确定用户是否已成功登录。 for eg. 例如

br = open("http://xyz.com/signin")
br['login'] = xxxx
br['password'] = xxx
result = br.submit().read()
f  = open('s.html', 'w')
f.write(result)
f.close

Now if login information is true s.html will contain next redirected page, and if information is false it will contain error page. 现在,如果登录信息为true,则s.html将包含下一个重定向页面,如果信息为false,则将包含错误页面。 Now how to find out that username annd pasword are true for a user......thanks in advance 现在如何找出用户名和密码对用户而言是正确的……预先感谢

You have to look at the page content that you were served. 您必须查看所提供的页面内容。

If it has the string 如果有字符串

login failed 登录失败

Or similar then you know it failed. 或类似的,那么你知道它失败了。

It usually redirects to a different page if login is successful else it remains on the same page and an error message appears 如果登录成功,通常会重定向到其他页面,否则它将保留在同一页面上并显示错误消息

You could use something like this 你可以用这样的东西

if br.geturl()==("https://address of the page after login"):
        print "SUCCESS"

You could use br.geturl() and check if it's the URL when you login or not. 您可以使用br.geturl()并在登录时检查它是否是URL。 Let's say it's xyz.com/welcome . 假设它是xyz.com/welcome

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM