NEW! |一次 API 调用即可获得干净数据,无需自行抓取。免费领取 10,000 个令牌 →

Selenium Python (4)。如何将 Selenium 与 Python 和代理结合使用

探索 Python 中 Selenium 的强大功能,并利用代理实现高效的网页抓取和自动化。使用 FineProxy 经济实惠的代理服务器,提升您的数据收集能力。

发布于2024年6月4日
阅读时间1 分钟

使用 Selenium 和 Python 进行网页抓取或自动化时,使用代理对于避免 IP 禁令和提高数据收集效率至关重要。本文将指导您在 Selenium 中使用 Python 设置和使用代理。

什么是 Selenium 以及为什么使用代理?#

Selenium 是一个强大的自动化网络浏览器工具,常用于网络抓取和测试网络应用程序。 代理 是将最终用户与他们浏览的网站分开的中介服务器,有助于掩盖 IP 地址和管理请求负载。

使用代理设置 Selenium#

在深入代码之前,请确保已安装必要的工具:

  • Python
  • 硒(pip install selenium)
  • 网络浏览器驱动程序(例如,Chrome 的 ChromeDriver 或 Firefox 的 GeckoDriver)

在 Selenium 中使用代理的分步指南#

1. 使用代理设置 Chrome#

要配置 Chrome 使用代理服务器,您需要在 Selenium 中设置所需的功能。

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

# Specify proxy details
proxy = "your_proxy_ip:port"

# Set up Chrome options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(f'--proxy-server={proxy}')

# Initialize WebDriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)

# Open a website to verify the proxy
driver.get("http://whatismyipaddress.com")

# Close the browser
driver.quit()

需要身份验证的代理#

如果您的代理需要身份验证,您可以使用 Proxy 来自的类 selenium.webdriver.common.proxy.

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

# Proxy settings
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = "your_proxy_ip:port"
proxy.ssl_proxy = "your_proxy_ip:port"
proxy.add_argument('--proxy-auth=user:password')

capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)

# Initialize WebDriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), desired_capabilities=capabilities)

# Open a website to verify the proxy
driver.get("http://whatismyipaddress.com")

# Close the browser
driver.quit()

使用代理服务器#

要配置 Firefox 使用代理,请修改 Firefox 配置文件设置。

from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager

# Proxy details
proxy_ip = "your_proxy_ip"
proxy_port = "port"

# Set up Firefox profile
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", proxy_ip)
profile.set_preference("network.proxy.http_port", int(proxy_port))
profile.set_preference("network.proxy.ssl", proxy_ip)
profile.set_preference("network.proxy.ssl_port", int(proxy_port))

# Initialize WebDriver
driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()), firefox_profile=profile)

# Open a website to verify the proxy
driver.get("http://whatismyipaddress.com")

# Close the browser
driver.quit()

常见问题的故障排除#

代理身份验证: 如果您的代理需要登录,请确保凭据设置正确。

IP封锁: 某些网站可能仍会阻止代理。使用住宅代理或轮换代理可缓解此问题。

摘要#

使用 Selenium 和 Python 的代理可以帮助您绕过 IP 限制并增强您的网络抓取项目。无论使用 Chrome 还是 Firefox,设置代理都涉及配置浏览器选项和功能以通过代理服务器路由流量。

浏览器设置方法身份验证可用的代码片段
ChromeChrome选项是是
FirefoxFirefox个人资料是是

通过遵循这些步骤,您可以确保您的抓取活动保持不间断和高效。如果您对未来的教程有任何疑问或想法,请随时发表评论!