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()
發佈了49 篇原創文章 · 獲贊 3 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章