使用 chromedriver 实现网络爬虫【手抄】

发布于:2025-04-17 ⋅ 阅读:(31) ⋅ 点赞:(0)

1、引用 selenium 包

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>4.29.0</version>
</dependency>
<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-chrome-driver</artifactId>
  <version>4.29.0</version>
</dependency>
<dependency>
  <groupId>org.openqa.selenium</groupId>
  <artifactId>selenium-support</artifactId>
  <version>4.29.0</version>
</dependency>

2、下载 chromedriver

下载地址:https://chromedriver.storage.googleapis.com/index.html

这里引用包支持版本为 114,我下载使用的是 114.0.5734.0,分为 windows版本以及 linux 版本。

114这个版本的 chromedriver 软件将放在源码中供大家学习使用。

3、对应的浏览器下载

由于chromedriver 最新版本为 114所以,最新的 chrome浏览器无法使用只能下载114这个版本chrome浏览器。

linux 下载地址:

https://www.googleapis.com/download/storage/v1/b/chromium-browser-snapshots/o/Linux%2F114059%2Fchrome-linux.zip?generation=1&alt=media

windows下载地址:

https://www.googleapis.com/download/storage/v1/b/chromium-browser-snapshots/o/Win_x64%2F1140629%2Fchrome-win.zip?generation=1683480562257014&alt=media

4、具体现实代码

public static void main(String[] args) throws IOException {

    // 设置 ChromeDriver 路径
    System.setProperty("webdriver.chrome.driver", "D:\\Project\\web-crawler\\chromedriver\\chromedriver.exe");

    // 配置 Chrome 浏览器
    ChromeOptions options = new ChromeOptions();
    // 如果不想显示浏览器界面
//    options.addArguments("--headless");
    // 设置启动时最大化浏览器窗口
    options.addArguments("--start-maximized");
    // 指定浏览器位置
    options.setBinary("D:\\Project\\web-crawler\\chrome-windows\\chrome.exe");
    WebDriver driver = new ChromeDriver(options);
    driver.get("http://182.131.3.xxx:8000/noticerpt/user/popup_login");

    // 显式等待,确保登录页面加载完成
    WebDriverWait loginWait = new WebDriverWait(driver, Duration.ofSeconds(30));
    // 获取背景图和拼图块
    // 拼图背景图
    WebElement button = loginWait.until(ExpectedConditions.presenceOfElementLocated(By.className("button4")));
    button.click();
    WebElement automv = loginWait.until(ExpectedConditions.presenceOfElementLocated(By.id("automv_1")));
    WebElement aBtn = automv.findElement(By.tagName("a"));
    aBtn.click();

    // 拼图块
    WebElement nav = driver.findElement(By.id("nav"));
    List<WebElement> elementList = nav.findElements(By.tagName("li"));
    WebElement navLi = elementList.get(1);
    WebElement navLiA = navLi.findElement(By.tagName("a"));
    navLiA.click();

    WebElement smsLoginFormsc = driver.findElement(By.id("smsLoginFormsc"));
    WebElement embedCaptchaSms = smsLoginFormsc.findElement(By.id("embed-captcha-sms"));
    WebElement gtSlider = embedCaptchaSms.findElement(By.className("gt_slider"));
    WebElement gtSliderKnob = gtSlider.findElement(By.className("gt_slider_knob"));
    new Actions(driver).moveToElement(gtSliderKnob).perform();


//    WebElement gtWidget = smsLoginFormsc.findElement(By.className("gt_widget"));
//    WebElement gtWidget = smsLoginFormsc.findElement(By.className("gt_widget"));
//    WebElement gtWidget = smsLoginFormsc.findElement(By.className("gt_widget"));

    // 获取拼图块的位置
//    Point sliderPosition = slider.getLocation();
//    System.out.println("拼图块的初始位置: " + sliderPosition);

//    // 计算缺失的拼图块位置(假设通过图像处理得出:x = 200, y = 0)
//    int targetX = 200;
//    int targetY = 0;
//    String imageUrl = backgroundElement.getAttribute("src");
//    BufferedImage background = ImageIO.read(new URL(imageUrl));
//    imageUrl = puzzlePieceElement.getAttribute("src");
//    BufferedImage puzzlePiece = ImageIO.read(new URL(imageUrl));
//
//    // 比较两张图片的差异
//    int diffX = 0;
//    int diffY = 0;
//
//    for (int y = 0; y < background.getHeight(); y++) {
//      for (int x = 0; x < background.getWidth(); x++) {
//        int bgPixel = background.getRGB(x, y);
//        int puzzlePixel = puzzlePiece.getRGB(x, y);
//
//        if (bgPixel != puzzlePixel) {
//          diffX = x;
//          diffY = y;
//          break;
//        }
//      }
//    }

    // 获取拼图块的当前位置
//    Point start = slider.getLocation();

//    // 模拟滑动拼图块
//    Actions actions = new Actions(driver);
//    actions.clickAndHold(slider)
//        .moveByOffset(targetX - start.getX(), targetY - start.getY())
//        .release()
//        .perform();

    // 等待拼图验证完成
//    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
//    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("success-message")));
//
//    System.out.println("拼图解锁成功!");

    // 关闭浏览器
//    driver.quit();

  }

以上代码主要就是实现关闭弹窗、显示滑动拼图验证码。其中代码 

http://182.131.3.xxx:8000/noticerpt/user/popup_login  xxx 为了安逸将最后一段修改为了XXX根据逻辑可以修改为自己的业务需求

5、总结

只是个人作为研究的手抄还是有很多不足,而且具体逻辑也没有写(但是会持续的更新)

6、代码

爬虫代码:维基框架/wiki-web-crawler

如果觉得还不错误,请支持一下作者开源框架 维基框架  维基代理