Jsoup的网页工具类
如果有相关问题,可以一起研究下
引入相关依赖
<!-- Html工具 -->
<dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.14.1</version>
</dependency>
代码片段
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;import java.io.*;
import java.nio.charset.StandardCharsets;/*** @Author: Climbing-pit* @Date: 2022/4/19 9:56* @Description: 网页工具类*/
public class HtmlUtil {/*** 读取本地HTML文件* 批量移除某个标签的某个属性* 重新写入* @param filePath HTML文件路径* @param tagName 标签名,例如<div></div>、<img></img>、<p></p>等等* @param attrName 属性名,例如id, class, src, style等等* @throws IOException*/public static void removeForTagAttr(String filePath, String tagName, String attrName) throws IOException {File in = new File(filePath);Document document = Jsoup.parse(in, "UTF-8");Elements imgs = document.getElementsByTag(tagName);for (Element img : imgs) {img.removeAttr(attrName);}write(in, document);}/*** 读取本地HTML文件* 批量修改某个标签的某个属性* 重新写入* @param filePath HTML文件路径* @param tagName 标签名,例如<div></div>、<img></img>、<p></p>等等* @param attrName 属性名,例如id, class, src, style等等* @throws IOException*/public static void editForTagAttr(String filePath, String tagName, String attrName, String attrValue) throws IOException {File in = new File(filePath);Document document = Jsoup.parse(in, "UTF-8");Elements imgs = document.getElementsByTag(tagName);for (Element img : imgs) {img.attr(attrName, attrValue);}write(in, document);}/*** 编辑完Document后写入* @param in 源文件* @param document 新的Document* @throws IOException*/public static void write(File in, Document document) throws IOException {//重新写入FileOutputStream fos = new FileOutputStream(in, false);OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);osw.write(document.html());osw.close();}public static void main(String[] args) {String filePath = "D:\\files\\practice\\testPaper\\1650335157835\\1650335157835.html";String tagName = "img";String attrName = "alt";try {HtmlUtil.removeForTagAttr(filePath, tagName, attrName);} catch (IOException e) {e.printStackTrace();}}
}
注意:jsoup是基于MIT协议发布的,可放心使用于商业项目。
Jsoup的网页工具类相关推荐
- 如何通过jsoup网络爬虫工具爬取网页数据,并通过jxl工具导出到excel
1:闲话少说,直接看需求: 抓取的url:http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=. ...
- [XML-Jsoup]Jsoup_对象的使用(Jsoup工具类,Document,Elements,Element,Node)
对象的使用: 1. Jsoup:工具类,可以解析html或xml文档,返回Document* parse:解析html或xml文档,返回Document* parse(File in, String ...
- Bootstrap——制作个人简历网页、工具类【边框(添加、删除、颜色、圆角)、清除浮动、颜色(文本、链接、背景)、display属性、浮动、定位、文本对齐】
制作个人简历网页 代码: <!DOCTYPE html> <html><head><meta charset="UTF-8">< ...
- JAVA 对接支付宝的 网页支付、当面付 工具类
maven pom需要引入的jar <!--支付宝支付--> <dependency><groupId>com.alipay.sdk</groupId> ...
- Java网页爬虫--基于URLConnection的网页爬虫工具类
在这个数据为王的时代,爬虫应用地越来越广泛,对于一个萌新程序员来说如果你要做爬虫,那么Python是你的不二之选.但是对于那些老腊肉的Java程序员(亦或者你是程序媛)想使用Java做爬虫也不是不行, ...
- jsoup html转义处理,jsoup解析网页出现转义符问题
https://www.oschina.net/question/996055_136438 *************************************** 我要解析这个网页 htt ...
- JAVA WEB之XSS防御工具类代码示例
简述 人们经常将跨站脚本攻击(Cross Site Scripting)缩写为CSS,但这会与层叠样式表(Cascading Style Sheets,CSS)的缩写混淆.因此,有人将跨站脚本攻击缩写 ...
- 免费IP代理池定时维护,封装通用爬虫工具类每次随机更新IP代理池跟UserAgent池,并制作简易流量爬虫...
前言 我们之前的爬虫都是模拟成浏览器后直接爬取,并没有动态设置IP代理以及UserAgent标识,这样很容易被服务器封IP,因此需要设置IP代理,但又不想花钱买,网上有免费IP代理,但大多都数都是不可 ...
- Android 开源控件与常用开发框架开发工具类
Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...
最新文章
- mysql注入攻击与防御word_SQL注入防御与绕过的几种姿势
- 分布式系统用户登录路由
- [css] 写出你知道的CSS水平和垂直居中的方法
- 点赞!一个程序员花了14小时寻找问题疫苗的流向
- react Link跳转无效_react常见的问题及解决办法
- StringUtils 的常用方法
- 运维讨厌流浪地球的原因
- python查找字符串出现次数_python实现从字符串中找出字符1的位置以及个数的方法...
- ftp登录常用命令和工作模式简介
- 用这几种方式清理Mac缓存,你的Mac就不会卡了
- Monkey工具详解
- 过年别再逼婚了,《黑镜》里的AI相亲系统是对爱情最好的匹配
- python生成模拟微信气泡图片
- linux中的kill 终止进程
- 图书管理系统(C文件读、存)
- 关于支付宝申请APP支付和网页支付在申请时需要的材料
- java廖雪峰_廖雪峰Java教程学习笔记(一)——Java程序基础
- 信息化企业招投标,最加分的资质证书大盘点
- php文章cms插件,Phpcms v9百度神马后台勾选文章推送插件
- 适合闷骚程序员的输入法皮肤