cnvd 获取最新cookie (反爬)

import time,sys
from selenium  import webdriver

from selenium.webdriver.chrome.options import Options



def get_cookie():
    options = webdriver.ChromeOptions()
    options.add_argument(' headless')
    options.add_argument(' no-sandbox')
    options.add_experimental_option('w3c', False)  # 获取linux 获取UA ,日志报错解决

    url = 'https://www.cnvd.org.cn/flaw/show/CNVD-2021-93994'
    caps = {
        'browserName': 'chrome',
        'loggingPrefs': {
            'browser': 'ALL',
            'driver': 'ALL',
            'performance': 'ALL',
        },
        'goog:chromeOptions': {
            'perfLoggingPrefs': {
                'enableNetwork': True,
            },
            'w3c': False,
        },
    }
    # windows linux
    if sys.platform=='win32':
        print('windows 系统')
        driver = webdriver.Chrome(desired_capabilities=caps) # desired_capabilities=caps 解决获取UA异常
    else:
        print('否则 linux')
        options = webdriver.ChromeOptions()
        options.add_argument(' headless')
        options.add_argument(' no-sandbox')
        options.add_experimental_option('w3c', False)  # 获取日志报错解决
        driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver',options=options,desired_capabilities=caps)
    
    # 解决访问cnvd 没有网络
    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": """
          Object.defineProperty(navigator, 'webdriver', {
            get: () => undefined
          })
        """
    })
    driver.get(url)
    import json
    try:
        logs = [json.loads(log['message'])['message'] for log in driver.get_log('performance')]
    except:
        print('获取UA异常')
    # print(type(logs))
    if logs:
        for i in logs:
            if i.get('params').get('headers'):
                UA = i.get('params').get('headers').get('User-Agent')
                print(UA)
                break
    driver.refresh()
    # print(dir(driver.get_log))
    time.sleep(10)
    cookies = ""
    for cookie in driver.get_cookies():
        cookies += u"%s=%s; " % (cookie["name"], cookie["value"])
    print(cookies)
    driver.close()
    
get_cookie()
import  sys,os

sys.path.append(os.getcwd()[:os.getcwd().index('cnvd')])
print(sys.path)

  

上一篇:selenium3+python3使用webdriver基本方法


下一篇:利用Redis撤销JSON Web Token产生的令牌