After the Python Selenium simulated login is successful, when you use this cookie and use the requests library for get, you will be prompted to "log in illegally".

one. Overview of steps

 a. Seleniumhttp://xk.suibe.edu.cn/xsxk/login.xk
 b. cookierequestssession:https://blog.csdn.net/big__v/article/details/78151940
 c. requestsposthttp://xk.suibe.edu.cn/xsxk/xkOper.xk
 d. 

two. (Chrmoe) screenshot of browser when logging in manually

1. request headerscookie



three. Overview of error conditions

1. cookiegetheadersget
2. Seleniumcookiesrequestssessiongett"" 

four. Troubleshooting

1. getcookiegetheaderspost
2. cookie"" "" 

five. Question guess

  1. guess that cookie may have been encrypted
  2. guess that there are other anti-crawler methods
  3. guess the timeliness of cookie

six. The complete code is as follows

-sharp -*- coding:utf-8 -*-
from selenium import webdriver
import requests
from selenium.webdriver.support.wait import WebDriverWait

-sharp 
codes = []
while True:
    code = raw_input(u":")
    if code != "":
        codes.append(code)
    else:
        break

-sharp 
driver = webdriver.Chrome()
url = "http://xk.suibe.edu.cn/xsxk/login.xk"
s = requests.session()
while True:
    driver.get(url)
    name_input = driver.find_element_by_id("username")  -sharp 
    pass_input = driver.find_element_by_id("password")  -sharp 
    login_button = driver.find_element_by_xpath("//*[@id="loginForm"]/table/tbody/tr[4]/td[2]/input[1]")
    name_input.clear()
    name_input.send_keys("1*******")  -sharp 
    pass_input.clear()
    pass_input.send_keys("********")  -sharp 
    WebDriverWait(driver, 300000000).until_not(lambda x: x.find_element_by_id("verifyCode").is_displayed())  -sharp 
    if u"" in driver.page_source:
        print u"!"
        selenium_cookies = driver.get_cookies()  -sharp seleniumcookies
        -sharp print(selenium_cookies)
        driver.close()
        break
    else:
        driver.close()

-sharp cookie
s = requests.Session()
for i in selenium_cookies:
    requests.utils.add_dict_to_cookiejar(s.cookies, {i["name"]: i["value"]})

-sharp post
for code in codes:
    info = {"method": "handleQxgxk",
            "jxbid": "201820191" + code,
            "glJxbid": "",
            "xyjc": ""}
    r = s.get("http://xk.suibe.edu.cn/xsxk/xkOper.xk", params=info)
    if "false" in r.text:
        print ":%s" % code, r.text
    else:
        print ":%s" % code

since it is a POST form, you should change session.get () to session.post () , and change the value of params= to data= .

in addition, you can open wireshark packet capture to make sure that the last cookie, you get from the browser is the same as the one you sent with requests.

if it still doesn't work, try adding User-Agent and Referer request header parameters to session.post () .


problem solved-there is a second validation problem (I don't know what it's called, let's just call it that, if there's anything wrong, please point it out in the comments. )
at first guessed that there was something wrong with the coding-after all, the coding of python2.7 was a bit of a headache. After various debugging, it is found that it is not a coding problem.
continues to analyze the browser from scratch and finds such a get, as shown in the figure:

guesses that this is "secondary authentication".
decisively add the following code to the code:

check = s.get('http://xk.suibe.edu.cn/xsxk/xkjs.xk?pyfaid=04265&jxqdm=2&data-frameid=main&data-timer=2000&data-proxy=proxy.xk', headers=headers)

and then it succeeds.

Menu