您的位置:首页 > 编程语言 > Python开发

python-spider

2015-08-27 17:50 597 查看
#!/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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: