初探Java爬虫

Updated on with 0 views and 0 comments

WebDriver

Webdriver 是一个远程控制接口,支持用户代理的自检和控制。 它提供了一个平台和语言中立的连接协议,作为进程外程序远程指示 web 浏览器行为的一种方式。

提供了一组接口来发现和操作 web 文档中的 DOM 元素,并控制用户代理的行为。 它的主要目的是允许 web 作者编写测试,从一个单独的控制过程中自动化一个用户代理,但也可以用于允许浏览器内的脚本控制一个(可能是单独的)浏览器。

IE浏览器驱动下载地址:http://docs.seleniumhq.org/download/

Firfox浏览器驱动下载地址:https://github.com/mozilla/geckodriver/releases

Chrome浏览器驱动下载地址:http://chromedriver.storage.googleapis.com/index.html

Edge浏览器驱动下载地址:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

本人使用的是chrome,驱动版本与浏览器版本对应关系如下(进入下载链接后选择版本点击notes.txt可以看到适用的版本)

ChromeDriver VersionChromeVersion
78.0.3904.1178
77.0.3865.4077
77.0.3865.1077
76.0.3809.12676
76.0.3809.6876
76.0.3809.2576
76.0.3809.1276
75.0.3770.9075
75.0.3770.875
74.0.3729.674
73.0.3683.6873
72.0.3626.6972
2.4671-73
2.4671-73
2.4570-72
2.4469-71
2.4369-71
2.4268-70
2.4167-69
2.4066-68
2.3966-68
2.3865-67
2.3764-66
2.3663-65
2.3562-64

selenium

Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla FirefoxSafari,Google Chrome,Opera等。

依赖

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

Demo

public class Spider {
    /**
    * @Description: testDemo测试的是拉勾网
    * @Param:
    * @return:
    * @Date: 2020/3/21
    */
    public static void main(String[] args) {
        //设置webdriver路径
        System.setProperty("webdriver.chrome.driver",Spider.class.getClassLoader().getResource("chromedriver").getPath());
        //创建webdriver
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.lagou.com/zhaopin/Java/?labelWords=label");

        //时间2020-3-21,拉钩网打开页面会提示恭喜被红包砸中,自动化关闭该页面
        WebElement noticeElement = webDriver.findElement(By.xpath("//div[@class='body-box']//div[@class='body-btn']"));

        clickOption(webDriver, "工作经验", "应届毕业生");
        clickOption(webDriver, "学历要求", "本科");
//        clickOption(webDriver, "融资阶段", "天使轮");
        clickOption(webDriver, "融资阶段", "不限");
        clickOption(webDriver, "公司规模", "不限");
        clickOption(webDriver, "行业领域", "移动互联网");

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

    }

    private static void extractJobsByPagination(WebDriver webDriver) {
        List<WebElement> jobItemListElement = webDriver.findElements(By.className("con_list_item"));
        for (WebElement jobItem : jobItemListElement) {
            String companyName = jobItem.findElement(By.className("company")).findElement(By.className("company_name")).getText();
            String money = jobItem.findElement(By.className("position")).findElement(By.className("money")).getText();
            System.out.println("公司名:"+companyName+";薪资:"+money);
        }
        //获取下一页按钮

        WebElement pagerNextBtn = webDriver.findElement(By.className("pager_next"));
        if (pagerNextBtn == null) {
            return;
        }
        if (!pagerNextBtn.getAttribute("class").contains("pager_next_disable")) {
            pagerNextBtn.click();
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            extractJobsByPagination(webDriver);
        }
    }

    private static void clickOption(WebDriver webDriver, String chooseTitle, String optionTitle) {
        //找到工作经验那一行的span
        WebElement chooseElement = webDriver.findElement(By.xpath("//li[@class='multi-chosen']//span[contains(text(),'" + chooseTitle + "')]"));
        //根据工作经验span找到对应的a标签
        WebElement optionElement = chooseElement.findElement(By.xpath("../a[contains(text(),'" + optionTitle + "')]"));
        //模拟点击事件
        optionElement.click();
    }
}