python - 学习模拟登录,卡在发送POST请求之后?
阿神
阿神 2017-04-18 10:21:50
0
4
877

学习模拟登录,发送了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)

以下是抓取的数据,是不是我的哪里找错了?

阿神
阿神

闭关修行中......

reply all(4)
Ty80

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:

import requests

url = ""
headers = {}
with requests.Session() as s:
    s.headers.update(headers)
    s.get(url)
    s.post(login_url)
PHPzhong

Let’s look at the requests document again, the a b c d variable names are eye-catching

Peter_Zhu

No cookie

f = requests.session() // 此时相当于打开了浏览器会话,接下来的操作都应该在这个会话里面执行,就不会有 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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!