python怎么登陆模拟_使用Python进行模拟登陆

# -*- 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要提交的数据可以使用浏览器自带的开发者工具分析,也可以使用一些抓包的工具

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值