Java 如何用新链接替换现有html文档中的链接?
我有一个由多个链接组成的HTML文档,我需要将此HTML文档中的链接更改为新的链接。 示例:输入html文档: https://stackoverflow.com“>堆栈溢出 https://stackoverflow1.com“>堆栈溢出1 输出html文档: 栈溢出 堆栈溢出1 我正在使用jsoup解析器从文档中获取所有链接的列表。 我在替换html文件中的链接时遇到了困难 以下是我的代码片段:运行代码后,我的test.html不会用新链接更新Java 如何用新链接替换现有html文档中的链接?,java,html,file,parsing,jsoup,Java,Html,File,Parsing,Jsoup,我有一个由多个链接组成的HTML文档,我需要将此HTML文档中的链接更改为新的链接。 示例:输入html文档: https://stackoverflow.com“>堆栈溢出 https://stackoverflow1.com“>堆栈溢出1 输出html文档: 栈溢出 堆栈溢出1 我正在使用jsoup解析器从文档中获取所有链接的列表。 我在替换html文件中的链接时遇到了困难 以下是我的代码片段:运行代码后,我的test.html不会用新链接更新 Path path = Paths.get("
Path path = Paths.get("test.html");
Charset charset = StandardCharsets.UTF_8;
Document doc;
try {
doc = Jsoup.parse(new File("test.html"), "UTF-8");
Element content = doc.getElementById("ExtractLinks");
Elements links = content.getElementsByTag("a");
for (Element link : links) {
String linkHref = link.attr("href");
System.out.println("URL:" + linkHref);
String fileContent = new String(Files.readAllBytes(path), charset);
fileContent = fileContent.replaceAll(linkHref, "www.google.com");
Files.write(path, fileContent.getBytes(charset));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这就是你的意思吗?你只需要JS
var-anchor_-tags=document.getElementsByTagName(“a”);
对于(变量i=0;i
您将链接放在哪里?是的,链接在锚标记内我不想要javascript代码:(我需要java代码