Skip to content
Beginner / 2 min read

如何在 Linux 上使用 ProxyScrape 代理与 Selenium 进行网页抓取(新手指南)

一份适合初学者的分步指南,介绍如何在 Linux 上的 Selenium 中配置 ProxyScrape 住宅、数据中心或移动代理。包括 HTTP 和 SOCKS5 设置、身份验证变通方法以及用于网页抓取的基本轮换。

Linux SOCKS5 HTTP(S) 网页抓取

概述

如果你一直在搜索“proxy scape”、“proxyscaper”或“proxyscape”,你很可能是在找 ProxyScrape。本指南使用正确的拼写。ProxyScrape 提供住宅、数据中心和移动代理,支持 HTTP 和 SOCKS5,拥有 9700 万+ IP 池,并提供按使用量付费和固定月付两种计费方式。

在本初学者教程中,你将在 Linux 上的 Selenium 中设置 ProxyScrape 代理来抓取网页。我们将涵盖 HTTP 和 SOCKS5、身份验证以及基本轮换。到最后,你将拥有一个可用的 Selenium 脚本,通过 ProxyScrape 代理路由流量。

前提条件

  • 一个 ProxyScrape 账户(免费试用或付费计划)。
  • Linux 系统(已在 Ubuntu/Debian 上测试)。
  • Python 3.6 或更高版本。
  • pip 包管理器。
  • 已安装 Google Chrome 或 Firefox。
  • 基本熟悉终端。

步骤

  1. 获取你的 ProxyScrape 代理凭据。

    登录你的 ProxyScrape 仪表板,并导航到你想要使用的代理类型(住宅、数据中心或移动)。记下以下内容:

    • 端点:host:port
    • 用户名和密码(如果启用了身份验证)

    对于网页抓取,住宅代理通常最适合避免被封禁,而数据中心代理速度更快且更便宜。

  2. 在 Linux 上安装 Selenium 和 WebDriver。

    安装 Selenium Python 包:

    pip install selenium
    

    安装 ChromeDriver(用于 Chrome/Chromium):

    sudo apt update
    sudo apt install chromium-chromedriver
    

    或者安装 GeckoDriver(用于 Firefox):

    sudo apt install firefox-geckodriver
    

    确保驱动程序在你的 PATH 中。你可以用 which chromedriver 检查。

  3. 编写一个使用 ProxyScrape HTTP 代理的基本 Selenium 脚本。

    创建一个名为 selenium_proxy.py 的文件,并添加以下代码。将占位符替换为你的 ProxyScrape 凭据。

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    PROXY = "http://username:password@host:port"  # e.g., http://user123:[email protected]:8080
    chrome_options = Options()
    chrome_options.add_argument(f'--proxy-server={PROXY}')
    driver = webdriver.Chrome(options=chrome_options)
    driver.get("https://httpbin.org/ip")
    print(driver.page_source)
    driver.quit()
    

    运行脚本:

    python selenium_proxy.py
    

    你应该会看到一个 JSON 响应,其中包含与你自己的 IP 不同的 IP 地址。如果显示的是你的真实 IP,则代理未正确工作。

  4. 处理 HTTP 代理的代理身份验证。

    Chrome 的 --proxy-server 标志不支持在 HTTP 代理中内联用户名/密码。对初学者来说,最简单的解决方案是在 ProxyScrape 仪表板中将你的 IP 加入白名单。转到你的代理设置,将你当前的公网 IP 添加到白名单,然后从代理 URL 中移除用户名/密码。

    或者,使用支持身份验证代理的 selenium-wire 库:

    pip install selenium-wire
    
    from seleniumwire import webdriver
    
    options = {
        'proxy': {
            'http': 'http://username:password@host:port',
            'https': 'https://username:password@host:port',
        }
    }
    driver = webdriver.Chrome(seleniumwire_options=options)
    driver.get("https://httpbin.org/ip")
    print(driver.page_source)
    driver.quit()
    
  5. 将 SOCKS5 代理与 Selenium 一起使用。

    ProxyScrape 也支持 SOCKS5。Chrome 可以使用内联身份验证的 SOCKS5:

    PROXY = "socks5://username:password@host:port"
    chrome_options.add_argument(f'--proxy-server={PROXY}')
    

    请注意,Firefox 可能需要对 SOCKS5 身份验证进行额外配置。

  6. 添加基本代理轮换。

    对于简单轮换,维护一个代理列表,并为每个浏览器会话随机选择一个。ProxyScrape 的住宅代理通常提供轮换端点,但你也可以手动轮换。

    import random
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    proxies = [
        "http://user:pass@host1:port",
        "http://user:pass@host2:port",
        # Add more proxies from your ProxyScrape list
    ]
    
    proxy = random.choice(proxies)
    chrome_options = Options()
    chrome_options.add_argument(f'--proxy-server={proxy}')
    driver = webdriver.Chrome(options=chrome_options)
    driver.get("https://httpbin.org/ip")
    print(driver.page_source)
    driver.quit()
    

    对于生产环境抓取,考虑使用代理轮换服务,或使用像 selenium-wire 这样带自定义中间件的库。

  7. 测试你的抓取脚本。

    编写一个简单的抓取器来获取页面并提取标题:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.by import By
    
    PROXY = "http://username:password@host:port"
    chrome_options = Options()
    chrome_options.add_argument(f'--proxy-server={PROXY}')
    driver = webdriver.Chrome(options=chrome_options)
    driver.get("https://example.com")
    print(driver.title)
    driver.quit()
    

    如果你看到页面标题,说明你的代理正在工作。

故障排除

  • 找不到 chromedriver:确保驱动程序已安装并在你的 PATH 中。如有需要,使用 export PATH=$PATH:/path/to/driver。
  • 代理连接被拒绝:验证你的凭据、端点和 IP 白名单。检查你的 ProxyScrape 计划是否有效。
  • Chrome 中弹出身份验证窗口:使用 IP 白名单或 selenium-wire 来避免弹出窗口。
  • 速度慢:尝试使用数据中心代理以获得更快的响应,或减少并发会话数量。
  • SSL 证书错误:在测试时向 Chrome 选项添加 --ignore-certificate-errors(不建议用于生产环境)。

总结

你已经学会了如何在 Linux 上使用 ProxyScrape 代理配合 Selenium 进行网页抓取。我们涵盖了获取凭据、安装 Selenium、设置 HTTP 和 SOCKS5 代理、处理身份验证以及基本轮换。请记住遵守网站服务条款和 robots.txt。对于更高级的设置,请探索 ProxyScrape 的轮换端点和会话控制功能。