Java爬虫爬简单网站系列(springboot)

<!--解析页面元素-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
​
<!--htmlunit解析js加载的动态数据页面--> https://mvnrepository.com/artifact/net.sourceforge.htmlunit/htmlunit -->
<dependency>
    <groupId>net.sourceforge.htmlunit</groupId>
    <artifactId>htmlunit</artifactId>
    <version>2.33</version>
</dependency>
​
<!--解析js加载的动态数据页面-->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.44.0</version>
</dependency>
​
<!--解析验证码,识图-->
<dependency>
    <groupId>net.sourceforge.tess4j</groupId>
    <artifactId>tess4j</artifactId>
    <version>4.5.3</version>
</dependency>

发出请求的方式

一、直接通过jsoup发起请求,缺点是不能爬取js动态加载的数据;

/**
 * 返回:Elements(标签下的内容)
 * 参数:url
 */
​
public static Elements getDoc(String HomeUrl, String divClassName) throws IOException {
    Document doc;
    try {
        SslUtils.ignoreSsl();
    } catch (Exception e) {
        e.printStackTrace();
    }
​
    doc = Jsoup.connect(HomeUrl).userAgent(
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36")
            .get();
​
    Elements select = doc.select(divClassName);
    return select;
}

二、根据htmlunit发起请求,可以解决上面的缺点,主要是模拟浏览器的环境

public static String htmlJsUtils(String url) {
    URL url1 = null;
    System.out.println("Loading page now-----------------------------------------------: " + url);
    // HtmlUnit 模拟浏览器
    WebClient webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getOptions().setJavaScriptEnabled(true);              // 启用JS解释器,默认为true
    webClient.getOptions().setCssEnabled(false);                    // 禁用css支持
    webClient.getOptions().setThrowExceptionOnScriptError(false);   // js运行错误时,是否抛出异常
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.getOptions().setTimeout(10 * 1000);                   // 设置连接超时时间
    try {
        url1 = new URL(url);
    } catch (MalformedURLException e) {
        e.printSta
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值