python-spider

来源:互联网 发布:淘宝卖家欺诈怎么解决 编辑:程序博客网 时间:2024/05/13 23:32
#!/usr/bin/env python#coding: utf-8import urllibimport urllib2import cookielibimport getpassdef login():    loginname = raw_input("Enter your username: ")    password = getpass.getpass("Enter your password: ")    filename = 'cookie.txt'    cookie = cookielib.MozillaCookieJar(filename)    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))    # loginpage = 'https://passport.guanaitong.com/index.php?wxA=Default.login'    loginurl = 'https://passport.guanaitong.com/index.php?wxA=Login.doEmployeeLogin'    imgurl = 'https://passport.guanaitong.com/index.php?wxA=Default.genVerifyCode'    headers = { 'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36',                'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',                'Accept-Language' : 'zh-CN,en-US;q=0.7,en;q=0.3',                'Accept-Encoding' : 'gzip, deflate',                'Connection' : 'keep-alive',                # 'Referer' : '',                'Content-Type' : 'application/x-www-form-urlencoded'    }    chk_img_req = urllib2.Request(imgurl)    chk_img_res = opener.open(chk_img_req)    try:        out = open('chkcode','wb')        out.write(chk_img_res.read())        out.flush()        out.close()        print('Get chk code Success!')    except IOError:        print('Get chk code Failed!')    chk_code = raw_input("Enter you check code: ")    postdata = {                'loginName' : loginname,                'password' : password,                'verifyCode' : chk_code            }    data = urllib.urlencode(postdata)    request = urllib2.Request(loginurl, data, headers)    login_result = opener.open(request)    cookie.save(ignore_discard=True, ignore_expires=True)    print login_result.read()if __name__ == '__main__':    login()
0 0
原创粉丝点击