# -*- coding: cp936 -*-
import urllib
import urllib2
import cookielib
def get_cookie(url):
try:
# 获取一个保存cookie的对象
cj = cookielib.LWPCookieJar()
# 将一个保存cookie对象,和一个HTTP的cookie的处理器绑定
cookie_support = urllib2.HTTPCookieProcessor(cj)
# 创建一个opener,将保存了cookie的http处理器,还有设置一个handler用于处理http的URL的打开
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)
# 将包含了cookie http处理器、http的handler的资源和urllib2对象绑定在一起
urllib2.install_opener(opener)
resp = urllib2.urlopen(url);
print "获取cookie成功!网络连接正常"
except Exception as e:
print "获取cookie失败!请检查网络设置"
healder = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.103 Safari/537.36',
'Referer':'http://121.248.104.139:8080/reader/redr_verify.php'}
postData = {'number':'123456',#账号
'passwd':'000000',#你的密码
'select':'cert_no',#特有参数,在本网站代表证件号,条码号,Email三个选项中的第一个选项
}
request_url = 'http://121.248.104.139:8080/reader/login.php'
login_url = 'http://121.248.104.139:8080/reader/login.php'
#模拟登陆
def main_action(loginurl,posturl,headlers,postData):
#调用函数获取cookie
get_cookie(loginurl) #loginUrl
print "正在执行模拟登陆请稍等......\n"
try:
postData = urllib.urlencode(postData)
#通过urllib2提供的request方法来向指定Url发送我们构造的数据,并完成登录过程
request = urllib2.Request(posturl, postData, headlers)
print request
response = urllib2.urlopen(request)
print response
text = response.read().decode("utf-8")
print text
print "模拟登陆成功"
except Exception as e:
print "模拟登陆失败!"
print e
main_action(login_url,request_url,healder,postData)#调用函数关于post要提交的数据可以使用浏览器自带的开发者工具分析,也可以使用一些抓包的工具