package htmlUnit;

import org.htmlunit.BrowserVersion;
import org.htmlunit.WebClient;
import org.htmlunit.WebConnection;
import org.htmlunit.WebRequest;
import org.htmlunit.WebResponse;
import org.htmlunit.html.HtmlAcronym;
import org.htmlunit.html.HtmlAnchor;
import org.htmlunit.html.HtmlDivision;
import org.htmlunit.html.HtmlElement;
import org.htmlunit.html.HtmlPage;
import org.htmlunit.html.HtmlParagraph;

import org.htmlunit.html.HtmlSpan;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

/**
 * htmlUnit测试
 * @Date: 2024-07-19 09:51
 * @Author: TangSong
 * @Description:
 */
public class HtmlUnitTest {



    @Test
    public  void testHtml() {
        WebClient webClient=null;
        try {
            webClient= new WebClient(BrowserVersion.CHROME);    //定义一个WebClient
            // 自定义一个处理404的处理器
            webClient.getOptions().setJavaScriptEnabled(false);
            webClient.getOptions().setTimeout(10000*6); // 设置超时时间为10秒
            final HtmlPage page=webClient.getPage("http://www.qinghai.gov.cn/zwgk/xwdt/qhyw/");    //从指定URL获取HtmlPage
            List<HtmlParagraph> divList=page.getByXPath("//p[@class='item']");
            int i = 0;
            for (HtmlParagraph node : divList) {
                System.out.println("***********************************************"+i);
                HtmlSpan span = node.getFirstByXPath("span[@class='gray']");
                if (span != null){
                    System.out.println(span.getTextContent());
                    String[] string = span.getTextContent().split("]");
                    System.out.println("[[["+string[0].replace("[","").replace("/","-").trim()+"1111");
                }
                HtmlAnchor anchors = node.getFirstByXPath("a[@target='_blank']");
                System.out.println(anchors.getAttribute("href"));
                System.out.println(anchors.getAttribute("title"));
                System.out.println("输出："+node.asXml());
                i++;
            }

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
         //   webClient.close();    //关闭客户端
        }
    }

}
