Python3网页post登陆

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python3网页post登陆相关的知识,希望对你有一定的参考价值。

[引入库]

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登陆的主要内容,如果未能解决你的问题,请参考以下文章

python使用cookie登陆网页

ECNUbash登陆校园网的方法

python 关于post和get的区别

python 关于post和get的区别

java模拟登陆时,post提交,对方网页提交时是鼠标onclick触发数据交流的,我该怎么弄?

python3爬取网页