java用xpath、regex、string提取html中的title的值
在Java中,您可以使用XPath、正则表达式和字符串操作来提取HTML中的title值。下面是每种方法的示例代码:
使用XPath:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class Main {
public static void main(String[] args) {
String html = "<html><head><title>Example Page</title></head><body><h1>Hello World</h1></body></html>";
Document document = Jsoup.parse(html);
Element titleElement = document.select("title").first();
String title = titleElement.text();
System.out.println(title);
}
}
使用正则表达式:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String html = "<html><head><title>Example Page</title></head><body><h1>Hello World</h1></body></html>";
Pattern pattern = Pattern.compile("<title>(.*?)</title>");
Matcher matcher = pattern.matcher(html);
if (matcher.find()) {
String title = matcher.group(1);
System.out.println(title);
}
}
}
使用字符串操作:
public class Main {
public static void main(String[] args) {
String html = "<html><head><title>Example Page</title></head><body><h1>Hello World</h1></body></html>";
int startIndex = html.indexOf("<title>") + 7;
int endIndex = html.indexOf("</title>");
String title = html.substring(startIndex, endIndex);
System.out.println(title);
}
}
无论您选择哪种方法,请确保在使用正则表达式或字符串操作时对HTML进行适当的处理和错误处理。此外,使用Jsoup库可以更方便地解析和处理HTML
原文地址: https://www.cveoy.top/t/topic/ijOD 著作权归作者所有。请勿转载和采集!