返回信息流楼主最近刚开始学习python QAQ
然后想写一个模拟登陆的脚本
现在想登陆 http://10.3.8.241/nav_login 这个网站
现在就是请求第一次页面之后 js中有一个checkcode 需要提取出来再加上自己的信息 学号密码啥的 再POST过去就可以登录
但是。。就是运行不成功 有点方。。。
我看了cookie也是加进去了啊 还有不是httplib2也是什么keep-alive这种的
下面贴一下浏览器下bp抓的包 还有python调试抓的包 还有我的代码
第一个包:
GET /nav_login HTTP/1.1
Host: 10.3.8.241
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://10.3.8.241/
Cookie: JSESSIONID=8F7DD457FDCB1CBE5B4A770EB5B3F516
Connection: keep-alive
Cache-Control: max-age=0
第二个登陆时候的
POST /LoginAction.action HTTP/1.1
Host: gwself.bupt.edu.cn
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://gwself.bupt.edu.cn/nav_login
Connection: keep-alive
Cookie: JSESSIONID=B1F479C2896813E6F9B23B638F942D7A; Path=
Content-Type: application/x-www-form-urlencoded
Content-Length: 107
code=&account=XXXXXXXXX&password=xxxxxxxxx&checkcode=381&Submit=%E7%99%BB+%E5%BD%95
再贴一下我开调试显示的抓包内容:
第一次:
connect: (10.3.8.241, 80) ************
send: 'GET /nav_login HTTP/1.1\r\nHost: 10.3.8.241\r\naccept-language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3\r\naccept-encoding: gzip, deflate\r\naccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nuser-agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0\r\nconnection: keep-alive\r\nreferer: http://gwself.bupt.edu.cn/nav_login\r\ncookie: \r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Server: Apache-Coyote/1.1
header: Set-Cookie: JSESSIONID=5352656CD023ACDAD1CDE2FBF9C9A4CD; Path=/
header: Content-Type: text/html;charset=UTF-8
header: Content-Length: 5586
header: Date: Tue, 22 Mar 2016 09:12:27 GMT
checkcode="2849";
2849
{'status': '200', 'content-length': '5586', 'content-location': 'http://10.3.8.241/nav_login', 'set-cookie': 'JSESSIONID=5352656CD023ACDAD1CDE2FBF9C9A4CD; Path=/', 'server': 'Apache-Coyote/1.1', 'date': 'Tue, 22 Mar 2016 09:12:27 GMT', 'content-type': 'text/html;charset=UTF-8'}
第二次的:
connect: (gwself.bupt.edu.cn, 80) ************
send: 'POST /LoginAction.action HTTP/1.1\r\nHost: gwself.bupt.edu.cn\r\nContent-Length: 108\r\naccept-language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3\r\naccept-encoding: gzip, deflate\r\naccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nuser-agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0\r\nconnection: keep-alive\r\nreferer: http://gwself.bupt.edu.cn/nav_login\r\ncookie: JSESSIONID=5352656CD023ACDAD1CDE2FBF9C9A4CD; Path=/\r\n\r\ncode=&account=XXXXXXXX&password=xxxxxxxxxx&checkcode=2849&Submit=%E7%99%BB+%E5%BD%95'
reply: 'HTTP/1.1 200 OK\r\n'
header: Server: Apache-Coyote/1.1
header: Content-Type: text/html;charset=UTF-8
header: Content-Length: 5586
header: Date: Tue, 22 Mar 2016 09:12:29 GMT
No exception was raised.
{'date': 'Tue, 22 Mar 2016 09:12:29 GMT', 'status': '200', 'content-length': '5586', 'content-type': 'text/html;charset=UTF-8', 'server': 'Apache-Coyote/1.1'}
#获取页面里面的checkcode
def getselfcode(page):
pattern = re.compile(r'checkcode=\".*\";')
match = pattern.search(page)
if match:
info = match.group()
print match.group()
else:
print "no match"
info = info.split('"')
return info[1]
#登陆
def getselfinfo():
http = httplib2.Http()
url = 'http://10.3.8.241/nav_login'
user_agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0'
values = {
'account' : 'XXXXXXXXX',
'password' : 'xxxxxxxxxxxx',
'code' : '',
'Submit' : '登 录',
'checkcode':'1234'
}
headers = {
'User-Agent' : user_agent,
'Referer' : 'http://gwself.bupt.edu.cn/nav_login',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'keep-alive',
'Cookie':''
}
response, content = http.request(url,'GET', headers = headers)
headers['Cookie'] = response['set-cookie']
values['checkcode'] = getselfcode(content)
print values['checkcode']
data = urllib.urlencode(values)
print response
print content
url = 'http://gwself.bupt.edu.cn/LoginAction.action'
try:
response, content = http.request(url,'POST', headers = headers, body = data)
except urllib2.URLError,e:
if hasattr(e, 'code'):
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
elif hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ', e.reason
else:
print 'No exception was raised.'
# everything is fine
print response
return content
求大神帮忙看看!还有什么其他的代码风格或者用法不好或者习惯不好什么的都请提出来!
这是一条镜像帖。来源:北邮人论坛 / python / #12807同步于 2016/3/22
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖
[问题]模拟登陆用户自助服务系统登不上 求大神帮看
meo
2016/3/22镜像同步5 回复
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
噢我用的request? 明天看看两个的区别
【 在 hansnow 的大作中提到: 】
: 请使用requests
:
: 通过『我邮2.0』发布
:
发自「贵邮」
一搜一片 http://search.icybee.cn/bbs/?key=python+%E7%99%BB%E5%BD%95%E8%84%9A%E6%9C%AC
哈哈哈 广告植入啊大神
【 在 icybee 的大作中提到: 】
: 一搜一片 http://search.icybee.cn/bbs/?key=python+登录脚本
:
发自「贵邮」