学习模拟登录,发送了post请求之后页面还是登录页面啊,不知道原因
import re
import requests
url='http://cer.imufe.edu.cn/authserver/login?service=http%3A%2F%2Fmy.imufe.edu.cn%2Findex.portal'
html=requests.get(url).text
lt_value=re.compile(r'name="lt" value="(.*?)"').search(html).group(1)
execution_value=re.compile(r'name="execution" value="(.*?)"').search(html).group(1)
dt={}
dt['username']='帐号'
dt['password']='密码'
dt['signIn']=''
dt['lt']=lt_value
dt['execution']=execution_value
dt['_eventId']='submit'
hd={}
hd['User-Agent']='Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
hd['Referer']=url
url2='http://my.imufe.edu.cn/detach.portal?.pmn=view&action=bulletinBrowser&.ia=false&.pen=pe1253&bulletinId=cf2509cf-9a54-11e6-86e2-7347af9c723b'
se=requests.session()
se.post(url,data=dt,headers=hd)
print(se.get(url2).text)
以下是抓取的数据,是不是我的哪里找错了?
You can directly use the Session context, and then all requests are completed through a session instance. The session object will automatically handle cookies for you. Of course, the content in the header must be defined by yourself when making the first request. The code structure is roughly as follows:
Let’s look at the requests document again, the a b c d variable names are eye-catching
No
cookie
requests.get
换成f.get
First of all, you need to understand the trust principle of the server after your login
The server will save a session value as a credential when you log in, and will return you a response header with a Set-Cookie field as a credential, so you need to send a get request with the cookie as your logged in credential.
It’s like you bought an electronic ticket online. People do know that you bought it, but how do you prove it is you when you enter the venue? You have to enter with the physical ticket they gave you after purchasing it
Back to the current scenario, the server knows that you are logged in and has saved the session (equivalent to saving the order data for buying tickets). At this time, when you send a get request, you need to prove that you are the one who just logged in. (Equivalent to requiring a physical ticket). At this time, you need to send the data in the Set-Cookie field returned to you by the server when logging in (take out your physical ticket to prove that you have purchased the ticket), and then the server can confirm your The identity will give you the corresponding data (equivalent to admission with a ticket)
Back to the specific implementation of the code, you need to check how the requests library brings the login cookie when sending a request, so that you can log in successfully