Selenium爬虫小案例

实现模拟人为操作自动化根据:工作经验、学历要求、公司规模、行业领域抓取拉勾网薪资范围;


1、下载 chromedriver

下载地址 : https://npm.taobao.org/mirrors/chromedriver/89.0.4389.23/

在这里插入图片描述

2、创建一个Maven项目;

在这里插入图片描述

然后向pom.xml导入selenium 依赖:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-server</artifactId>
    <version>3.141.59</version>
</dependency>

在这里插入图片描述

然后将我们之前下载的 chromedriver 丢到项目的resources目录下;

在这里插入图片描述

创建一个LagouSpider

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 * @ClassName: LagouSpider
 * @Author: lenovo
 * @Date: 2021/3/22 21:43
 * Version: 1.0
 */
public class LagouSpider {
    public static void main(String[] args) {
        // 1、设置webdriver的路径
        System.setProperty("webdriver.chrome.driver",LagouSpider.class.getClassLoader().getResource("chromedriver.exe").getPath());

        // 2、创建 webdriver
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.lagou.com/zhaopin/Java/?labelWords=label");
    }
}

在这里插入图片描述


当我们启动类的时候,他会自动使用浏览器访问拉钩的官网;

在这里插入图片描述

3、选择索引条件;

xpath文档:https://www.w3cschool.cn/xpath/xpath-syntax.html

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 * @ClassName: LagouSpider
 * @Author: lenovo
 * @Date: 2021/3/22 21:43
 * Version: 1.0
 */
public class LagouSpider {
    public static void main(String[] args) {
        // 1、设置 webdriver 的路径
        System.setProperty("webdriver.chrome.driver", LagouSpider.class.getClassLoader().getResource("chromedriver.exe").getPath());

        // 2、创建 webdriver
        WebDriver webDriver = new ChromeDriver();

        // 3、跳转页面
        webDriver.get("https://www.lagou.com/zhaopin/Java/?labelWords=label");
        webDriver.manage().window().maximize(); // 将浏览器最大化

        // 4、通过 xpath 选中元素
        clickOption(webDriver, "工作经验", "在校/应届");
        clickOption(webDriver, "学历要求", "本科");
        clickOption(webDriver, "融资阶段", "天使轮");
        clickOption(webDriver, "公司规模", "15-50人");
        clickOption(webDriver, "行业领域", "移动互联网");

    }

    /**
     * clickOption:单击选项
     * @param webDriver
     * @param choseTitle
     * @param optionTitle
     */
    private static void clickOption(WebDriver webDriver, String choseTitle, String optionTitle) {
        WebElement chosenElement = webDriver.findElement(By.xpath("//li[@class='multi-chosen']//span[contains(text(),'" + choseTitle + "')]"));
        WebElement optionElement = chosenElement.findElement(By.xpath("../a[contains(text(),'" + optionTitle + "')]"));
        optionElement.click();
    }

}


4、分析解析元素;

在这里插入图片描述


在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


在这里插入图片描述

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

/**
 * @ClassName: LagouSpider
 * @Author: lenovo
 * @Date: 2021/3/22 21:43
 * Version: 1.0
 */
public class LagouSpider {
    public static void main(String[] args) throws InterruptedException {
        // 1、设置 webdriver 的路径
        System.setProperty("webdriver.chrome.driver", LagouSpider.class.getClassLoader().getResource("chromedriver.exe").getPath());

        // 2、创建 webdriver
        WebDriver webDriver = new ChromeDriver();

        // 3、跳转页面
        webDriver.get("https://www.lagou.com/zhaopin/Java/?labelWords=label");
        webDriver.manage().window().maximize(); // 将浏览器最大化


        // 4、通过 xpath 选中元素
        clickOption(webDriver, "工作经验", "在校/应届");
        clickOption(webDriver, "行业领域", "移动互联网");



        // 5、解析页面元素
        List<WebElement> jobElements = webDriver.findElements(By.className("con_list_item"));
        for (WebElement jobElement : jobElements) {
            WebElement moneyElement = jobElement.findElement(By.className("position")).findElement(By.className("money"));
            System.out.print(moneyElement.getText() + "\t");
            String company_name = jobElement.findElement(By.className("company")).findElement(By.className("company_name")).getText();
            System.out.print(company_name +"\n");
        }


    }

    /**
     * clickOption:单击选项
     * @param webDriver
     * @param choseTitle
     * @param optionTitle
     */
    private static void clickOption(WebDriver webDriver, String choseTitle, String optionTitle) {
        WebElement chosenElement = webDriver.findElement(By.xpath("//li[@class='multi-chosen']//span[contains(text(),'" + choseTitle + "')]"));
        WebElement optionElement = chosenElement.findElement(By.xpath("../a[contains(text(),'" + optionTitle + "')]"));
        optionElement.click();
    }

}

分页获取数据:

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


可以跳转下一页

在这里插入图片描述


最后一页的下一页是灰色的;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

/**
 * @ClassName: LagouSpider
 * @Author: lenovo
 * @Date: 2021/3/22 21:43
 * Version: 1.0
 */
public class LagouSpider {
    public static void main(String[] args) throws InterruptedException {
        // 1、设置 webdriver 的路径
        System.setProperty("webdriver.chrome.driver", LagouSpider.class.getClassLoader().getResource("chromedriver.exe").getPath());

        // 2、创建 webdriver
        WebDriver webDriver = new ChromeDriver();

        // 3、跳转页面
        webDriver.get("https://www.lagou.com/zhaopin/Java/?labelWords=label");
        webDriver.manage().window().maximize(); // 将浏览器最大化


        // 4、通过 xpath 选中元素
        clickOption(webDriver, "工作经验", "在校/应届");
        clickOption(webDriver, "行业领域", "移动互联网");


        // 5、解析页面元素
        extractJobsByPagination(webDriver);


    }

    /**
     * 分页获取页面元素
     * ctrl alt m  --- 将代码块抽取成方法
     *
     * @param webDriver
     */
    private static void extractJobsByPagination(WebDriver webDriver) {
        List<WebElement> jobElements = webDriver.findElements(By.className("con_list_item"));
        for (WebElement jobElement : jobElements) {
            WebElement moneyElement = jobElement.findElement(By.className("position")).findElement(By.className("money"));
            String company_name = jobElement.findElement(By.className("company")).findElement(By.className("company_name")).getText();
            System.out.println(company_name + " : " + moneyElement.getText());
        }

        WebElement nextPageBtn = webDriver.findElement(By.className("pager_next"));
        if (!nextPageBtn.getAttribute("class").contains("pager_next_disabled")) {
            nextPageBtn.click();
            System.out.println("解析下一页");
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printstacktrace();
            }
            extractJobsByPagination(webDriver);
        }
    }

    /**
     * clickOption:单击选项
     *
     * @param webDriver
     * @param choseTitle
     * @param optionTitle
     */
    private static void clickOption(WebDriver webDriver, String choseTitle, String optionTitle) {
        WebElement chosenElement = webDriver.findElement(By.xpath("//li[@class='multi-chosen']//span[contains(text(),'" + choseTitle + "')]"));
        WebElement optionElement = chosenElement.findElement(By.xpath("../a[contains(text(),'" + optionTitle + "')]"));
        optionElement.click();
    }

}

在这里插入图片描述

项目演示:http://mtw.so/5X056Q

相关文章

转载地址:https://www.cnblogs.com/mini-monkey/p/12104821...
web自动化测试过程中页面截图相对比较简单,可以直接使用sel...
目录前言一、Selenium简介二、浏览器驱动1.浏览器驱动参考2....
一、iframe的含义:iframe是HTML中框架的一种形式,在对界面...
转载请注明出处❤️作者:测试蔡坨坨原文链接:caituotuo.to...
'''##**认识selenium**​**下载:pipinstall...