python selenium chrome如何设置请求体

王胖胖
  • 29

各位好,请问如何设置chrome的请求体呢?

问题来源为,破解极验验证码时的场景,第一次手动输入验证码,输入后网站会返回我一个ACCESS_TOKEN,这个token会在1个小时内失效,也就是我在一个小时内刷新页面,或者临近一个小时的时候刷新页面,会返回我一个新的token,也就是说我就登录一次,后面只需要刷新页面即可。

但是目前遇到一个问题,chrome会莫名其妙关闭,因此我需要在判断浏览器被关闭时重新启动浏览器,然后post如下的数据。

数据如下所示:

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

在Python 的selenium chrome中如何设置,才能请求成功呢?

回复
阅读 4.1k
1 个回答

已解决

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
            
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏