python-spider

#!/usr/bin/env python
#coding: utf-8

import urllib
import urllib2
import cookielib
import getpass


def 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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值