我需要动态获取 URL 的标题和描述。我需要使用什么才能做到这一点?
以以下 URL 为例:http://en.wikipedia.org/wiki/Stack_overflow
我需要提取 URL 的图 block 及其描述。您更喜欢如下所示的 jsoup 提取吗?
url.select("title");
如果是,如何提取url的描述?
最佳答案
我认为你需要一个像 Jericho 这样的 HTML 解析器。
看一下这个例子: http://jericho.htmlparser.net/samples/console/src/ExtractText.java
特别是这两种方法:
private static String getTitle(Source source) {
Element titleElement=source.getFirstElement(HTMLElementName.TITLE);
if (titleElement==null) return null;
// TITLE element never contains other tags so just decode it collapsing whitespace:
return CharacterReference.decodeCollapseWhiteSpace(titleElement.getContent());
}
private static String getMetaValue(Source source, String key) {
for (int pos=0; pos<source.length();) {
StartTag startTag=source.getNextStartTag(pos,"name",key,false);
if (startTag==null) return null;
if (startTag.getName()==HTMLElementName.META)
return startTag.getAttributeValue("content"); // Attribute values are automatically decoded
pos=startTag.getEnd();
}
return null;
}
关于java - 使用 URL 动态获取标题和描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9957099/