gpt4 book ai didi

java - 使用 URL 获取标题、元描述内容

转载 作者:搜寻专家 更新时间:2023-11-01 04:00:48 27 4
gpt4 key购买 nike

我正在尝试从 URL 中提取标题和元标记的描述内容,这就是我所拥有的:

fin[] //urls in a string array

for (int f = 0; f < fin.length; f++)
{
Document finaldoc = Jsoup.connect(fin[f]).get(); //fin[f] contains url at each instance
Elements finallink1 = finaldoc.select("title");
out.println(finallink1);
Elements finallink2 = finaldoc.select("meta");
out.println(finallink2.attr("name"));
out.println(fin[f]); //printing url at last
}

但它不打印标题,只是将描述打印为“描述”并打印 url。

结果:

描述
plus.google.com
发电机
en.wikipedia.org/wiki/google
描述
earth.google.com

最佳答案

你可以使用这个:

String getMetaTag(Document document, String attr) {
Elements elements = document.select("meta[name=" + attr + "]");
for (Element element : elements) {
final String s = element.attr("content");
if (s != null) return s;
}
elements = document.select("meta[property=" + attr + "]");
for (Element element : elements) {
final String s = element.attr("content");
if (s != null) return s;
}
return null;
}

然后:

 String title = document.title();
String description = getMetaTag(document, "description");
if (description == null) {
description = getMetaTag(document, "og:description");
}
// and others you need to
String ogImage = getMetaTag(document, "og:image")

....

关于java - 使用 URL 获取标题、元描述内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9958425/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com