码迷,mamicode.com
首页 > 编程语言 > 详细

【Python3网页post登陆】

时间:2017-05-26 00:54:53      阅读:523      评论:0      收藏:0      [点我收藏+]

标签:logs   str   main   spin   family   www   jsp   encode   imp   

[引入库]

import urllib
import urllib.parse
import urllib.request
import http.cookiejar

[请求头,通过FireFox查得]

headers = {    Accept: text/html, application/xhtml+xml, image/jxr, */*,
                Referer: http://jxpt.cuit.edu.cn/eol/homepage/common/index.jsp,
                Accept-Language: zh-CN,
                User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko,
                Content-Type: application/x-www-form-urlencoded,
                Accept-Encoding: gzip, deflate,
                Content-Length: 57,
                Host: jxpt.cuit.edu.cn,
                Connection: Keep-Alive,
                Pragma: no-cache,
                }

[需要post的数据]

postData = {  IPT_LOGINUSERNAME: 账号,
              IPT_LOGINPASSWORD: 密码,
            }                    

[获取cookie]

#输入账号密码的地址
loginURL = http://jxpt.cuit.edu.cn/eol/homepage/common/index.jsp
#自动记住cookie
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
urllib.request.install_opener(opener)# 安装opener到全局
resp = urllib.request.urlopen(loginURL)

[post登陆]

#post数据地址
postURL = http://jxpt.cuit.edu.cn/eol/homepage/common/login.jsp
#数据编码
postData = urllib.parse.urlencode(postData).encode(utf-8)
#构造请求
request = urllib.request.Request(postURL, postData, headers)
#发送请求
response = urllib.request.urlopen(request)

[打印返回的信息]

#respInfo = response.info()
#接收返回的信息
html = response.read().decode(gb2312)
print(html)
print("over!")
    

[完整代码]

 1 import urllib
 2 import urllib.parse
 3 import urllib.request
 4 import http.cookiejar
 5 
 6 
 7 def login():
 8     
 9     headers = {    Accept: text/html, application/xhtml+xml, image/jxr, */*,
10                 Referer: http://jxpt.cuit.edu.cn/eol/homepage/common/index.jsp,
11                 Accept-Language: zh-CN,
12                 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko,
13                 Content-Type: application/x-www-form-urlencoded,
14                 Accept-Encoding: gzip, deflate,
15                 Content-Length: 57,
16                 Host: jxpt.cuit.edu.cn,
17                 Connection: Keep-Alive,
18                 Pragma: no-cache,
19                 }
20 
21     postData = {IPT_LOGINUSERNAME: 账号,
22                 IPT_LOGINPASSWORD: 密码,
23                 }
24 
25     #输入账号密码的地址
26     loginURL = http://jxpt.cuit.edu.cn/eol/homepage/common/index.jsp
27     #自动记住cookie
28     cj = http.cookiejar.CookieJar()
29     opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
30     urllib.request.install_opener(opener)# 安装opener到全局
31     resp = urllib.request.urlopen(loginURL)
32 
33 
34     #post数据地址
35     postURL = http://jxpt.cuit.edu.cn/eol/homepage/common/login.jsp
36     #数据编码
37     postData = urllib.parse.urlencode(postData).encode(utf-8)
38     #构造请求
39     request = urllib.request.Request(postURL, postData, headers)
40     #发送请求
41     response = urllib.request.urlopen(request)
42 
43 
44     respInfo = response.info()
45     #接收返回的信息
46     html = response.read().decode(gb2312)
47     print(html)
48     print("over!")
49 
50 if __name__ == __main__:
51     login()

 

【Python3网页post登陆】

标签:logs   str   main   spin   family   www   jsp   encode   imp   

原文地址:http://www.cnblogs.com/lemonno/p/6906340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!