gpt4 book ai didi

jsoup - 如何在java中使用Regex获取像facebook这样的页面元(标题,描述,图像)附加网址

转载 作者:行者123 更新时间:2023-12-03 23:24:52 24 4
gpt4 key购买 nike

如何在 .java 中使用 Regex 获取页面元数据(标题、描述、图像),例如 facebook 附加 url

最佳答案

这是一个片段,它读取网页并构建一小块 HTML 来显示 Open Graph 图像,并在右侧环绕图像的标题。如果缺少 OG 标签,它会回退到仅使用 html 标题,因此它可以代表所有网页。

public static String parsePageHeaderInfo(String urlStr) throws Exception {

StringBuilder sb = new StringBuilder();
Connection con = Jsoup.connect(urlStr);

/* this browseragant thing is important to trick servers into sending us the LARGEST versions of the images */
con.userAgent(Constants.BROWSER_USER_AGENT);
Document doc = con.get();

String text = null;
Elements metaOgTitle = doc.select("meta[property=og:title]");
if (metaOgTitle!=null) {
text = metaOgTitle.attr("content");
}
else {
text = doc.title();
}

String imageUrl = null;
Elements metaOgImage = doc.select("meta[property=og:image]");
if (metaOgImage!=null) {
imageUrl = metaOgImage.attr("content");
}

if (imageUrl!=null) {
sb.append("<img src='");
sb.append(imageUrl);
sb.append("' align='left' hspace='12' vspace='12' width='150px'>");
}

if (text!=null) {
sb.append(text);
}

return sb.toString();
}

关于jsoup - 如何在java中使用Regex获取像facebook这样的页面元(标题,描述,图像)附加网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11656064/

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