代理刮刀 Selenium - 将 chromedriver 更改为 geckodriver

Proxy Scraper Selenium - changing chromedriver to geckodriver

提问人:tormaoen 提问时间:11/10/2023 更新时间:11/10/2023 访问量:42

问:

我有这个代码可以抓取我找到的代理,但它使用 Chromedriver 而不是 Geckodriver

from selenium import webdriver
from selenium.webdriver.common.by import By
import chromedriver_autoinstaller # pip install chromedriver-autoinstaller

chromedriver_autoinstaller.install() # To update your chromedriver automatically
driver = webdriver.Chrome()

# Get free proxies for rotating
def get_free_proxies(driver):
    driver.get('https://sslproxies.org')

    table = driver.find_element(By.TAG_NAME, 'table')
    thead = table.find_element(By.TAG_NAME, 'thead').find_elements(By.TAG_NAME, 'th')
    tbody = table.find_element(By.TAG_NAME, 'tbody').find_elements(By.TAG_NAME, 'tr')

    headers = []
    for th in thead:
        headers.append(th.text.strip())

    proxies = []
    for tr in tbody:
        proxy_data = {}
        tds = tr.find_elements(By.TAG_NAME, 'td')
        for i in range(len(headers)):
            proxy_data[headers[i]] = tds[i].text.strip()
        proxies.append(proxy_data)
    
    return proxies


free_proxies = get_free_proxies(driver)

有人可以向我解释如何编辑它以与 Firefox/Geckodriver 兼容吗?/\

基本同上。

python selenium-webdriver firefox 代理 webdriver

评论


答:

0赞 Mate Mrše 11/10/2023 #1

首先安装 webdriver 管理器(它将为您省去手动下载 geckodriver 的痛苦):

pip install webdriver-manager

然后,将脚本的第一部分(直到第 7 行)更改为:

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

driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))