某网址获取头部信息Csrf-Token值

发布于:2025-06-30 ⋅ 阅读:(16) ⋅ 点赞:(0)
class yqp():
    def __init__(self):
        self.url = 'https://www/xxxx/jjcgJjggJbxx/preBiddingNoticeList? '
        self.CsrfToken_url = 'https://www/xxxx/csrf/getToken'
        self.CsrfToken_headers = {
            "Host": "//www/xxxx",
            "Cache-Control": "max-age=0",
            "sec-ch-ua": '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": '"Windows"',
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "11",
            "Accept": "11",
            "Sec-Fetch-Site": "none",
            "Sec-Fetch-Mode": "navigate",
            "Sec-Fetch-User": "?1",
            "Sec-Fetch-Dest": "document",
            "Accept-Encoding": "gzip, deflate, br, zstd",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cookie": "HWWAFSESID=7d05c73bef2f7cb691; HWWAFSESTIME=1734399235101; BENSESSCC_TAG=7d05c73bef2f7cb691"
        }

        response = requests.get(self.CsrfToken_url)
        CsrfToken = response.json()['data']
        self.headers = {
            'Host':'//www/xxxx',
            'Content-Length': '105',
            'Accept': 'application/json, text/plain, */*',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Connection': 'keep-alive',
            'Content-Type': 'application/json',
            'Cookie': 'HWWAFSESID=9d73873c10e822d6c1; HWWAFSESTIME=1688624324020; BENSESSCC_TAG=9d73873c10e822d6c1',
            'Csrf-Token': CsrfToken,
            'Origin': '//www/xxxx',
            'Referer': '//www/xxxx',
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-origin',
            'User-Agent': '11',
            'sec-ch-ua': '11',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
                }

        self.chrome_options = Options()
        self.chrome_options.add_argument("--headless")
        self.browser = webdriver.Chrome(options=self.chrome_options)
        self.browser.set_page_load_timeout(200)
        self.browser.set_script_timeout(200)


    def fenduan(self, screenshot_content, img, img_1):
        left = screenshot_content.location['x']
        top = screenshot_content.location['y']
        right = screenshot_content.location['x'] + screenshot_content.size['width']
        bottom = screenshot_content.location['y'] + screenshot_content.size['height']
        img_p = img.crop((left, top, right, bottom))  # 对浏览器截图进行裁剪
        img_p.save(img_1)

    def denglu(self):
        error = 0
        link = 'https://www/xxxx/#/login'  
        while True:
            self.browser.get(link)
            # self.browser.find_element_by_xpath('//*[@id="gform"]/div/h4/span[2]').click() 
            time.sleep(0.5)
            dl_click = self.browser.find_element_by_xpath('//*[@id="app"]/div/div[1]/div/div/div[2]/div/div[2]/div[1]/div[2]/div[1]').click()
            user_input = self.browser.find_element_by_xpath('//*[@id="app"]/div/div[1]/div/div/div[2]/div/div[2]/form/div[1]/div/div/input')
            pw_input = self.browser.find_element_by_xpath('//*[@id="app"]/div/div[1]/div/div/div[2]/div/div[2]/form/div[2]/div/div/input')
            user_input.send_keys("11")
            pw_input.send_keys("11")
            width = 1000
            height = 1500
            # width = self.browser.execute_script("return document.body.scrollWidth")
            # height = self.browser.execute_script("return document.body.scrollHeight")
            self.browser.set_window_size(width, height)
            self.browser.get_screenshot_as_file('11')
            time.sleep(1)
            img = Image.open('11')
            img_1 = '11'
            screenshot_content = self.browser.find_element_by_xpath('//*[@id="app"]/div/div[1]/div/div/div[2]/div/div[2]/form/div[3]/div/div[2]/img')
            self.fenduan(screenshot_content, img, img_1)
            with open(img_1, 'rb') as file:
                binary_data_1 = file.read()

            first = YdmVerify.common_verify(binary_data_1)

            # yanzm_1 = df.loc[first[1], first[0].upper()]

            a = self.browser.find_element_by_xpath('//*[@id="app"]/div/div[1]/div/div/div[2]/div/div[2]/form/div[3]/div/div[1]/input')
            a.clear()
            a.send_keys(first)
            self.browser.find_element_by_xpath('//*[@id="app"]/div/div[1]/div/div/div[2]/div/div[2]/div[2]/button').click()  # 点击登录
            time.sleep(2)
            # self.browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[4]/div[1]/ul/li[2]/a').click() #30条
            # self.browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[4]/div[1]/ul/li[3]/a').click() #50条
            if '11' in self.browser.page_source:
                print('登录成功')
                break
            else:
                self.browser.quit()
                error += 1
                if error > 7:
                    break
                continue
        cookies = self.browser.get_cookies()

        temp_list = []
        for cookie in cookies:
            temp_list.append('{}={}'.format(cookie['name'], cookie['value']))
        CookieStr = '; '.join(temp_list)
        self.headers['Cookie'] = CookieStr
        # self.con_headers['Cookie'] = CookieStr
        self.headers['Cookie'] = CookieStr
        # self.cookie = CookieStr
        # self.browser.quit()


    def run_1(self):
        self.denglu()
        self.start()

    def run(self,num):
        current_time = datetime.datetime.now().strftime('%Y-%m-%d')
        data={"citys":"","provinces":"","noticeName":"","publishTimeBegin":current_time,"publishTimeEnd":current_time}
        # data={"citys":"","provinces":"","noticeName":"","publishTimeBegin":'2025-06-24',"publishTimeEnd":'2025-06-24'}
        params = {
            'pageNum': num,
            'pageSize': '20',
        }
        res = requests.post(self.url, headers=self.headers, json=data, verify=False, params=params,
                            timeout=10).json()
        data_list = res['data']['list']
        return data_list

部分代码 仅供自己记录参考