当前位置: 首页 > 工具软件 > PHP-spider > 使用案例 >

python-spider

单于旭东
2023-12-01
#!/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()
 类似资料: