# -*- coding: utf-8 -*- # @Author: wangkun # @Time: 2022/12/5 import json import os import sys import time from selenium.webdriver import DesiredCapabilities from selenium import webdriver from selenium.webdriver.chrome.service import Service sys.path.append(os.getcwd()) class GetCookies: @classmethod def get_cookies(cls, channel): channel_url = 'https://haokan.baidu.com/tab/'+str(channel) ca = DesiredCapabilities.CHROME ca['goog:loggingPrefs'] = {'performance': 'ALL'} chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('headless') chrome_options.add_argument(f'user-agent=Mozilla/5.0 (Windows NT 10.0; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36') chrome_options.add_argument('--no-sandbox') driver = webdriver.Chrome(desired_capabilities=ca, options=chrome_options) # driver = webdriver.Chrome(desired_capabilities=ca, options=chrome_options, # service=Service('/Users/wangkun/Downloads/chromedriver_v107/chromedriver')) driver.implicitly_wait(10) driver.get(channel_url) time.sleep(5) logs = driver.get_log('performance') driver.quit() for line in logs: msg = json.loads(line['message']) if 'params' not in msg['message']: pass elif 'headers' not in msg['message']['params']: pass elif 'Set-Cookie' not in msg['message']['params']['headers']: pass else: cookies = msg["message"]['params']['headers']['Set-Cookie'] return cookies if __name__ == '__main__': print(GetCookies.get_cookies('recommend'))