简体   繁体   English

python selenium chrome,如何设置浏览器的请求主体

[英]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. 问题的源头是第一次,手动滑动输入验证码后,网站输入后将返回ACCESS_TOKEN,令牌将在1小时后过期,我在一个小时内还是刷新页面,快一个小时后才刷新页面,我将返回一个新令牌,即我将登录一次,后面只需刷新页面即可。

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. 但是现在出现了一个问题,Chrome将以某种方式关闭,因此在判断浏览器处于关闭状态时,我需要重新启动浏览器,然后按以下方式发布数据。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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