使用JSoup从URL获取og:image

时间:2019-02-02 15:59:04

标签: coldfusion jsoup

我不知道我在做什么错。我正在尝试使用JSOUP和Coldfusion获得og:image网址。

<cfhttp method="get" url="http://www.bbc.com/culture/story/20150304-is-house-of-cards-worth-watching" result="theresult">


<cfscript>
    // Create the jsoup object
    Jsoup = createObject("java", "org.jsoup.Jsoup");

    // HTML string
    html = "#theresult.filecontent#";
    // Parse the string
    document = Jsoup.parse(html);
    // Extract content
    title = document.title();
    metaOgImage  = document.select("meta[property=og:image]").first();

    writeOutput("
        <div>Title: #title#</div>       
        <div>Meta: #metaOgImage#</div>
    ");
</cfscript>

1 个答案:

答案 0 :(得分:1)

  

metaOgImage = document.select("meta[property=og:image]").first();

返回一个Element,代表<meta>标签。要仅显示“内容”属性(该页面存储URL的位置),请尝试:

<div>Meta: #metaOgImage.attr("content")#</div>

请记住,metaOgImage可能为null(如果未找到),因此请确保在CF代码中添加针对此处理。