问题
I'm trying to use Mechanize to get emails from my Outlook web client, but I'm having troubles logging in. It gives me the errors listed below. I've verified that the user name and password are correct. Any ideas?
Here is my code:
import mechanize
b = mechanize.Browser()
cj = cookielib.LWPCookieJar()
b.set_cookiejar(cj)
b.open('https://mail.example.com/owa/')
br.select_form("logonForm")
b['username'] = 'myname'
b['password'] = 'password'
b.submit()
I can see that form components are being accessed correctly, but after submitting, the login page displays again with two errors:
- The user name or password you entered isn't correct. Try entering it again.
- Please enable cookies for this Web site.
I thought the b.set_cookiejar(cj)
would take care of cookies. Could this be the root of my problem?
回答1:
import mechanize
import cookielib
br = mechanize.Browser()
br.set_handle_robots( False )
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
br.open('https://webmail.server.com')
br.select_form(nr = 0)
br.form['username'] = 'username'
br.form['password'] = 'password'
br.submit()
Use this it works for me
来源:https://stackoverflow.com/questions/25612539/mechanize-owa-user-password-error