在ratelimi产量的同时用硒进行XMLhttprequest后处理的有效方法

2024-04-29 11:39:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个脚本,使用Selenium登录并通过网站导航到目标页面,然后使用POST-HTTPRequest提交表单以及验证码解析

我想使用HTTPRequest的原因是,该站点几乎不可能获得完成和提交表单所需的确切元素。(xPath和CSS也不起作用)

我的问题是我怎样才能做得更好?我怎样才能放弃利率限制,这样账户就不会有麻烦了

这是我需要提交表单的代码的一部分

def work(self):
    try:
        proxy = self.get_proxy()
        self.chrome_options.add_argument('--proxy-server=socks://{}'.format(proxy))
        self.driver = webdriver.Chrome(chrome_options=self.chrome_options, executable_path=self.exe_path)
        self.driver.get(self.web_url)
        ua = UserAgent()
        if self.is_visible('g-recaptcha') is True:
            cookies = pickle.dump(self.driver.get_cookies())
            js = '''
            var piggy = new XMLHttpRequest();
            piggy.open('POST', 'https://website.com', true);
            piggy.setRequestHeader(
            'Accept', 'application/json, text/plain, */*';
            'User-Agent', '{}'.format(au.random);
            'Content-Type', 'application/json';
            'Referer', 'https://website.com';
            'Cookie', '{}'.format(cookies);
            );
            piggy.send('"info": "{}".format('Some Info'), "captcha": "{}".format(self.captoken());
            return this.responseText
            };
            '''
            result = self.driver.execute_script(js)
            print(result)
    except Exception as e:
        print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)

Tags: pathselfformat表单getisdriverjs