python自動ログインサイト


# -*- coding:UTF-8 -*-
'''
Created on 2010-5-8

@author:     
'''


import urllib2

def login(name,ps):
    opener=urllib2.build_opener(urllib2.HTTPCookieProcessor())
    opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3')]
    urllib2.install_opener(opener)
    #01 login
    data='name=%s&password=%s' % (name,ps)
    req = urllib2.Request(url='http://www.iteye.com/login',data=data)
    f = urllib2.urlopen(req)
    print "login..."
    assert 200 == f.code
    
    req = urllib2.Request(url='http://app.iteye.com/profile')
    f = urllib2.urlopen(req)
    assert 200 == f.code
    reposne =  f.read()
    if '<p class="flash_error">' in reposne:
        print '    '
    else:
        print '    '
    
    
if __name__ == '__main__':
    
    login('    ','123456')