Proxy Scraper Selenium - 将 chromedriver 更改为 geckodriver

问题描述 投票:0回答:1

我有这段代码可以抓取我发现的代理,但是它使用 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 proxy webdriver
1个回答
0
投票

首先安装 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()))
© www.soinside.com 2019 - 2024. All rights reserved.