You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何并行运行需可视化Chrome窗口的Selenium网页爬虫?

解决方案:可视化Chrome爬虫的并行与远程部署

一、本地多实例并行运行

既然无法使用无头模式,本地可通过多进程启动独立Chrome实例实现并行,避免单实例串行的速度瓶颈。

1. 基于multiprocessing的多进程实现

Chrome为独立进程,多进程模式能绕过Python GIL限制,更适合资源隔离的浏览器实例运行。修改你的代码如下:

import multiprocessing
import time
import random
from selenium import webdriver

def crawl_single_url(url):
    # 每个进程独立初始化配置和驱动
    options = webdriver.ChromeOptions()
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument('--proxy-server={"........"}')
    options.add_argument('window-size=500x250')
    options.add_experimental_option('useAutomationExtension', False)

    def interceptor(request):
        request.headers['Referer'] = 'https://www.*****'
        # 补充其他需要设置的请求头

    driver = webdriver.Chrome("D:\chromedriver\94\chromedriver.exe", options=options)
    driver.request_interceptor = interceptor

    try:
        driver.get(url)
        # 执行保存JSON到CSV的逻辑
        time.sleep(2 * random.random())
    finally:
        # 确保资源释放
        driver.stop_client()
        driver.close()
        driver.quit()

if __name__ == "__main__":
    listurl = ["www...", "www..."]
    # 根据电脑配置设置并行数量(如4-8个,避免内存/CPU过载)
    with multiprocessing.Pool(processes=4) as pool:
        pool.map(crawl_single_url, listurl)

2. 关键注意事项

  • 每个进程必须独立初始化Chrome配置和驱动,禁止全局共享driver实例
  • 若使用代理,需为每个实例分配独立代理IP,避免多实例共用IP触发反爬
  • 并行数量需匹配硬件配置,过高会导致系统卡顿、浏览器崩溃

二、远程多系统部署Chrome实例

可以通过Selenium Grid实现跨机器的Chrome实例调度,把多台机器作为执行节点,统一分配爬虫任务。

1. Selenium Grid部署步骤

  • Hub调度节点:在一台机器上启动Hub,作为任务分发中心

    java -jar selenium-server-standalone-<对应版本号>.jar -role hub
    
  • Node执行节点:在每台要运行Chrome的机器上启动Node,连接到Hub
    Windows环境示例:

    java -jar selenium-server-standalone-<对应版本号>.jar -role node -hub http://<Hub机器IP>:4444/grid/register -browser "browserName=chrome,maxInstances=4"
    

    其中maxInstances=4表示该节点最多同时运行4个Chrome实例,可根据机器配置调整。

  • 修改爬虫代码连接Hub:

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    def crawl_with_remote(url):
        options = webdriver.ChromeOptions()
        options.add_argument('--no-sandbox')
        options.add_argument('--disable-dev-shm-usage')
        options.add_argument('--proxy-server={"........"}')
        options.add_argument('window-size=500x250')
        options.add_experimental_option('useAutomationExtension', False)
    
        def interceptor(request):
            request.headers['Referer'] = 'https://www.*****'
            # 补充其他请求头设置
    
        # 连接到远程Hub节点
        driver = webdriver.Remote(
            command_executor='http://<Hub机器IP>:4444/wd/hub',
            desired_capabilities=DesiredCapabilities.CHROME,
            options=options
        )
        driver.request_interceptor = interceptor
    
        try:
            driver.get(url)
            # 执行数据保存逻辑
            time.sleep(2 * random.random())
        finally:
            driver.quit()
    

    配合本地多进程代码,即可把任务分发到各个远程节点的Chrome实例。

2. 辅助工具选项

  • 云浏览器平台:如BrowserStack、Sauce Labs,提供现成的远程浏览器集群,无需自行维护机器,适合大规模爬虫需求(需付费)
  • Playwright:若可替换工具,Playwright原生支持多实例并行与远程部署,可视化模式下性能优于Selenium,但需重构爬虫代码

三、额外优化建议

  • 尽量复用Chrome实例:若网站反爬允许,可在一个实例内连续爬取多个URL,减少浏览器启动/关闭的开销(需定期重启实例避免内存泄漏)
  • 禁用非必要功能:添加--disable-images参数关闭图片加载(需测试是否影响页面正常请求与Referer有效性),降低资源占用

内容的提问来源于stack exchange,提问作者Ben Hendel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 03:05:48