Java操作Html

由于工作需要,需要解析使用java操作Html页面,所以搜索了一下资料进行汇总。

一、java解析Html

java解析Html需要引入jsoup的包,这里的httpClient模拟请求使用。

复制代码
        <dependency>
            <!-- jsoup HTML parser library @ https://jsoup.org/ -->
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
复制代码

代码如下:

复制代码
public static void main(String[] args) {
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet("https://www.baidu.com/");
        try {
            httpGet.setHeader("Content-Type","text/plain;charset=UTF-8");

            HttpResponse httpResponse =  httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            String resHtml = EntityUtils.toString(httpEntity,"UTF-8");

            // 使用 jsoup 解析
            Document doc = Jsoup.parse(resHtml);
            Elements e = doc.select("input[id=su]");
            System.out.println(e);
            System.out.println(e.val());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
复制代码

我用java代码获取的如下图的元素:

输出结果为:

二、使用htmlunit模拟请求

需要引入下面jar包

        <!-- https://mvnrepository.com/artifact/net.sourceforge.htmlunit/htmlunit -->
        <dependency>
            <groupId>net.sourceforge.htmlunit</groupId>
            <artifactId>htmlunit</artifactId>
            <version>2.35.0</version>
        </dependency>

代码如下:

复制代码
package com.wh.utils;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import java.io.*;

/**
 * @Description //TODO
 * @Author wanghao
 * @Date 2019-08-05 21:45
 **/
public class HttpAnalysisHtml {


    public static void main(String[] args) {
        // 创建webclient
        WebClient webClient = new WebClient();
        // 取消 JS 支持
        webClient.getOptions().setJavaScriptEnabled(false);
        // 取消 CSS 支持
        webClient.getOptions().setCssEnabled(false);

        // 获取指定网页实体
        try {
            HtmlPage page = (HtmlPage) webClient.getPage("https://www.baidu.com/");
            // 获取搜索输入框
            HtmlInput input = (HtmlInput) page.getHtmlElementById("kw");
            // 往输入框 “填值”
            input.setValueAttribute("王浩");
            // 获取搜索按钮
            HtmlInput btn = (HtmlInput) page.getHtmlElementById("su");
            // “点击” 搜索
            HtmlPage page2 = btn.click();


            FileOutputStream fos = new FileOutputStream(new File("D:\\repository\\code\\util\\src\\main\\resources\\data\\test002.html"));
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            osw.write(page2.asXml());
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


}
复制代码

 

 

 

 

 

 

参考文档:

https://blog.csdn.net/larger5/article/details/79683048

 https://blog.csdn.net/zhanglei500038/article/details/74858395

posted @   苦心明  阅读(6800)  评论(1编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示