无法使用urllib2登录

时间:2013-08-22 22:50:46

标签: python urllib2

我正在尝试使用python登录我的网站帐户。此代码适用于Facebook,但不知道为什么不使用att.com。在页面重定向期间有一个弹出窗口,不确定缺少什么。

import urllib
import urllib2

class ATT_LOGIN(object):

    def __init__(self, login, password):
        """ Start up... """
        self.login = login
        self.password = password

        self.cj = cookielib.CookieJar()
        self.opener = urllib2.build_opener(
            urllib2.HTTPRedirectHandler(),
            urllib2.HTTPHandler(debuglevel=0),
            urllib2.HTTPSHandler(debuglevel=0),
            urllib2.HTTPCookieProcessor(self.cj)
        )
        self.opener.addheaders = [
            ('User-agent', ('Mozilla/4.0 (compatible; MSIE 6.0; '
                           'Windows NT 5.2; .NET CLR 1.1.4322)'))
        ]

        # need this twice - once to set cookies, once to log in...
        self.loginToATT()
        print self.loginToATT()

    def loginToATT(self):
        """
        Handle login. This should populate our cookie jar.
        """
        login_data = urllib.urlencode({
            'userid' : self.login,
            'password' : self.password,
        })
        response = self.opener.open("https://www.att.com/olam/loginAction.doview", login_data)
        return ''.join(response.readlines())

0 个答案:

没有答案