python登录网页的两种方式

2023-01-31 04:01:49 方式 登录 两种

以登录我校系统为例:
1. 观察传输的包,填写内容登录

#_*_ coding:utf-8 _*_
import requests

url = 'https://cas.xjtu.edu.cn/login'
header = { 'User-agent': 'Mozilla/5.0 (windows NT 10.0; WOW64) AppleWEBKit/537.36 (Khtml, like Gecko) Chrome/45.0.2454.85 Safari/537.36'
}
#这是我从chrome f12里点登录后看到的传输的内容
from_data = {'username': 'xxx',
    'passWord': 'xxx',
    'code': '',
    'lt': 'xxx',
    'execution': 'e1s1',
    '_eventId': 'submit',
    'submit': '登录'
}
s = requests.session()
response = s.post(url, data = from_data, headers = header)
print response.status_code

2.利用cookie直接上

#_*_ coding:utf-8 _*_
import requests

url = 'Https://cas.xjtu.edu.cn/login;jsessionid=084E35D36A4DA796BBB9A12E5E221BBE?service=http://my.xjtu.edu.cn/Login'
header = { 'User-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36',
    'Cookie': 'xxx'
}

s = requests.session()
response = s.post(url, headers = header)
print response.status_code 

相关文章