10行python模拟登陆一个网站

/ 0评 / 0
#coding=utf-8
def login(username, password):
    user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
    cj = cookielib.MozillaCookieJar()
    #cj.load(filename='mycookie.txt', ignore_discard=True, ignore_expires=True)
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    url = 'http://passport.hupu.com/login'
    postdata = 'username=' + username + '&password='+ password +'&rememberme=1&charset=utf-8'
    req = urllib2.Request(url, postdata)
    res = opener.open(req).read()
    '''保存cookie'''
    cj.save(filename='mycookie.txt', ignore_discard=True, ignore_expires=True)

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注