HtmlUnit、Jsoup、webmagic基本介绍

HtmlUnit官网:https://htmlunit.sourceforge.io/
Jsoup官网:https://jsoup.org/cookbook/cleaning-html/safelist-sanitizer
Selenium官网 https://www.selenium.dev/zh-cn/documentation/webdriver/getting_started/

什么是Jsoup

在这里插入图片描述
可以抓取页面,但是更多的时候我们用来解析HTML。

Document doc = Jsoup.connect("https://en.wikipedia.org/").get();
log(doc.title());
Elements newsHeadlines = doc.select("#mp-itn b a");
for (Element headline : newsHeadlines) {
  log("%s\n\t%s", 
    headline.attr("title"), headline.absUrl("href"));
}

什么是HtmlUnit

在这里插入图片描述
是一个无GUI浏览器,允许您调用页面、填写表单、单击链接。
可以用作自动化测试、爬虫等,可以抓取动态页面,但是对js和css支持不好。

// 创建一个web客户端
final WebClient webClient = new WebClient(BrowserVersion.CHROME);
// 不支持js
webClient.getOptions().setJavaScriptEnabled(false);
// 抓取页面
final HtmlPage page = webClient.getPage("https://www.cnblogs.com/starzhai/p/14106770.html");
// 分析html
System.out.println(page.getTitleText());
Object firstByXPath = page.getFirstByXPath("//*[@id=\"cnblogs_post_body\"]/p[15]");
System.out.println(firstByXPath);

Selenium

参考 https://www.cnblogs.com/tester-ggf/p/12602211.html#2257972919
Selenium是一系列基于Web的自动化工具,提供一套测试函数,用于支持Web自动化测试。函数非常灵活,能够完成界面元素定位、窗口跳转、结果比较。

	@Test
    public void testChromeSearch() throws InterruptedException {
        // Optional. If not specified, WebDriver searches the PATH for chromedriver.
        System.setProperty("webdriver.chrome.driver", "E:\\doc\\chromedriver_win32\\driver\\chromedriver.exe");
        // 1.创建webdriver驱动
        WebDriver driver = new ChromeDriver();
        // 2.打开百度首页
        driver.get("https://www.baidu.com");
        // 3.获取输入框,输入selenium
        driver.findElement(By.id("kw")).sendKeys("selenium");
        // 4.获取“百度一下”按钮,进行搜索
        driver.findElement(By.id("su")).click();
        // 5.退出浏览器
    }

    @Test
    public void testOperadriverSearch() throws InterruptedException {
        // Optional. If not specified, WebDriver searches the PATH for chromedriver.
        System.setProperty("webdriver.opera.driver", "E:\\doc\\driver\\operadriver.exe");
        // 1.创建webdriver驱动
        WebDriver driver = new OperaDriver();
        // 2.打开百度首页
        driver.get("https://www.baidu.com");
        // 3.获取输入框,输入selenium
        driver.findElement(By.id("kw")).sendKeys("selenium");
        // 4.获取“百度一下”按钮,进行搜索
        driver.findElement(By.id("su")).click();
        // 5.退出浏览器
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值