首页 > 代码库 > 【Python3网页post登陆】

【Python3网页post登陆】

[引入库]

import urllibimport urllib.parseimport urllib.requestimport 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 =http://www.mamicode.com/ {  IPT_LOGINUSERNAME: 账号,              IPT_LOGINPASSWORD: 密码,            }                    

[获取cookie]

#输入账号密码的地址loginURL = http://jxpt.cuit.edu.cn/eol/homepage/common/index.jsp#自动记住cookiecj = 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 = http://www.mamicode.com/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 = http://www.mamicode.com/{IPT_LOGINUSERNAME: 账号,22                 IPT_LOGINPASSWORD: 密码,23                 }24 25     #输入账号密码的地址26     loginURL = http://jxpt.cuit.edu.cn/eol/homepage/common/index.jsp27     #自动记住cookie28     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.jsp36     #数据编码37     postData = http://www.mamicode.com/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登陆】