(娱乐)爬虫工具

36 篇文章 0 订阅
1 篇文章 0 订阅

htmlunit: 

package com.example.demo2;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.ProxyConfig;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.HttpHostConnectException;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description:
 * @author: kaiba
 * @date: 2019/12/27
 */
@Component
public class PaChongUtils {
    private static WebClient webClient = new WebClient(BrowserVersion.BEST_SUPPORTED);
    /**
     * openUrl 方法
     * 说明:
     * 创建人:yangkai
     * @param url:
     * @return com.gargoylesoftware.htmlunit.html.HtmlPage
     * @throws
     */
    public static HtmlPage openUrl(String url,ProxyConfig proxyConfig){
        webClient.getOptions().setJavaScriptEnabled(true);              // 启用JS解释器,默认为true
        webClient.getOptions().setCssEnabled(false);                    // 禁用css支持
        webClient.getOptions().setThrowExceptionOnScriptError(false);   // js运行错误时,是否抛出异常
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setTimeout(10*1000);                   // 设置连接超时时间
        webClient.getOptions().setUseInsecureSSL(true);                 // 启用ssl
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());//很重要,设置支持AJAX
        if(proxyConfig!=null){
            webClient.getOptions().setProxyConfig(proxyConfig);
        }else {

        }
        webClient.waitForBackgroundJavaScript(500);  // 等待js后台执行0.5秒
        try {
            HtmlPage htmlPage = webClient.getPage(url);
            return htmlPage;
        }catch (ConnectTimeoutException e){
            e.printStackTrace();
            return null;
        }catch (HttpHostConnectException e){
            e.printStackTrace();
            return null;
        }catch (SocketTimeoutException e){
            e.printStackTrace();
            return null;
        }catch (IOException e){
            e.printStackTrace();
            return null;
        }
    }

    // 正则匹配
    public static List<String> getMatherSubstrs(String str, String regex) {
        List<String> list = new ArrayList<String>();
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        while (m.find()) {
            list.add(m.group());
        }
        return list;
    }
}

依赖:

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.sourceforge.htmlunit/htmlunit -->
        <dependency>
            <groupId>net.sourceforge.htmlunit</groupId>
            <artifactId>htmlunit</artifactId>
            <version>2.29</version>
        </dependency>

 jsoup:

        InetSocketAddress addr =  new  InetSocketAddress("223.199.17.183",9999);
        Proxy HTTPproxy =  new  Proxy(Proxy.Type. HTTP , addr); // http 代理
        Document document = Jsoup.connect("https://blog.csdn.net/qq_36441163/article/details/105995743")
                .timeout(5*1000)
                .proxy(null)
                .get();
        JXDocument jxd = new JXDocument(document.getElementsByTag("html"));
        Element div = (Element) jxd.selOne("//*[@id=\"content_views\"]");
        List<Element> ps=div.getElementsByTag("p");
        for (int i = 0; i < ps.size(); i++) {
            System.out.println(ps.get(i).text());
        }

依赖:

        <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.12.1</version>
        </dependency>
        <dependency>
            <groupId>cn.wanghaomiao</groupId>
            <artifactId>JsoupXpath</artifactId>
            <version>2.2</version>
        </dependency>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值