【爬虫】selenium打开浏览器以及页面

本篇探讨如何使用 selenium 打开浏览器

selenium 基础与网页打开

selenium 是一个广泛应用于自动化测试和网页抓取的工具,它能够模拟用户在浏览器中的各种操作。首先,我们需要根据指定的浏览器类型(这里以 Chrome 为例)打开网页。以下是相关代码示例:

python 复制代码
from selenium import webdriver


def web_create(self, web_type, value):
    """
    打开网页
    :param web_type: 浏览器类型
    :param value: 网页地址
    :return: 网页对象
    """
    if web_type.lower() == "chrome":
        # 设置Chrome选项,包括隐藏Selenium特征、设置代理IP和排除或关闭一些Selenium相关开关
        # 参考:https://blog.csdn.net/XianZhe_/article/details/120929106
        options = webdriver.ChromeOptions()
        options.add_experimental_option('excludeSwitches', ['enable-automation'])
        options.add_argument('--disable-blink-features=AutomationControlled')
        options.add_argument('--disable-extensions')
        # options.add_argument('--disable-gpu')
        # options.add_argument('--disable-infobars')
        options.add_argument('--disable-notifications')
        # options.add_argument('--disable-popup-blocking')
        # options.add_argument('--disable-web-security')
        # options.add_argument('--ignore-certificate-errors')
        # options.add_argument('--no-sandbox')
        # 最大化窗口
        options.add_argument('--start-maximized')
        # 无痕浏览模式
        options.add_argument('--incognito')
        # options.add_argument('--user-data-dir=/dev/null')
        # options.add_argument('--proxy-server={}'.format(proxy_address + ':' + proxy_port))
        # options.add_argument('--proxy-auth={}:{}'.format(proxy_username, proxy_password))
        # options.add_experimental_option('excludeSwitches', ['enable-automation', 'useAutomationExtension'])

        driver = webdriver.Chrome(options=options)  # 创建Chrome浏览器驱动实例
        # 隐藏navigator.webdriver标志,将其值修改为false或undefined
        driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
            'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
        })
        # 设置user-agent,改变user-agent的值
        user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
        driver.execute_cdp_cmd("Network.setUserAgentOverride", {"userAgent": user_agent})

        driver.get(value)  # 打开指定的网页地址
        return driver
    else:
        raise ValueError("目前只支持chrome类型的浏览器")

在上述代码中,我们创建了 web_create 函数。它首先判断浏览器类型是否为 chrome,如果是,则创建 Chrome 浏览器驱动实例。接着,通过执行 CDP 命令来隐藏 navigator.webdriver 标志,修改 user-agent,以此来规避一些网站对 selenium 的检测。最后,打开指定的网页地址并最大化窗口,返回浏览器驱动对象,以便后续操作。

所有打开浏览器参数参考:

List of Chromium Command Line Switches << Peter Beverloo

相关推荐
lightqjx3 分钟前
【数据结构】顺序表(sequential list)
c语言·开发语言·数据结构·算法
巨人张12 分钟前
信息素养Python编程题
开发语言·python
站大爷IP18 分钟前
Python爬虫动态IP代理报错全解析:从问题定位到实战优化
python
hie9889424 分钟前
CentOS环境搭建-快速升级G++版本
linux·python·centos
盛寒28 分钟前
向量空间 线性代数
python·线性代数·机器学习
阿猿收手吧!36 分钟前
【计算机网络】HTTP1.0 HTTP1.1 HTTP2.0 QUIC HTTP3 究极总结
开发语言·计算机网络
JAVA学习通37 分钟前
图书管理系统(完结版)
java·开发语言
paishishaba44 分钟前
处理Web请求路径参数
java·开发语言·后端
七七七七071 小时前
C++类对象多态底层原理及扩展问题
开发语言·c++