简体   繁体   中英

python selenium chrome,How to set up the browser's request body

I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.

The source of the problem for the first time, manual sliding input verification code input after the site will return a ACCESS_TOKEN, the token will expire after 1 hours, I was in an hour or refresh the page, when approaching an hour to refresh the page, I will return a new token, that is I'll log in once, behind only need to refresh the page.

But now there is a problem, chrome will be closed somehow, so I need to restart browser when judging browser is turned off, then post data as follows.

post header like this:

POST https://www.test.com/rest.php HTTP/1.1
Host: api.test.com
Connection: keep-alive
Content-Length: 216
Origin: https://api.test.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36
Content-type: application/x-www-form-urlencoded
Accept: */*
Referer: https://api.test.com/nspcross.html
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cookie: CASLOGIN=true; CASLOGINSITE=1; LOGINACCSITE=1

nsp_svc=AppPromote.Developer.getRole&access_token=CFpS9d%2FDFoxQvOiM%2B%2F3j1iFce0dYDYQR0qq7TfAVUG5e%2FGhgBx2jHL6p8M02y09V%2FEHQpwemI7V1ACD32ERFD45678FGTHDBW3EI6iX4%3D&nsp_fmt=JSON&nsp_ts=18547841246
import os
import re
import psutil
import seleniumrequests
#判断浏览器状态
def handle_brower_statue():
    while True:
        brower_list = []
        pids = psutil.pids()
        pid_name_search = re.compile(r'chrome')
        for pid in pids:
            p = psutil.Process(pid)
            pid_name_chrome = re.search(pid_name_search, p.name())
            if pid_name_chrome == None:
                pass
            else:
                brower_list.append(pid)
        print len(brower_list)
        if len(brower_list) < 8:
            for kill_pid in brower_list:
                os.system("kill -9 %s" % kill_pid)
            access_token = read_access_token()
            new_data = {
                "nsp_svc":"AppPromote.Developer.getRole",
                "access_token":access_token,
                "nsp_fmt":"JSON",
                "nsp_ts":setting.nsp_ts,
            }
            new_driver = seleniumrequests.Chrome()
            time.sleep(2)
            response = new_driver.request('POST','https://api.xxxx.com/rest.php',data=new_data)
            print response.text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM