Search code examples
coldfusionjsoup

Get og:image from url using JSoup


I don't know what I am doing wrong here. I am tring to get the og:image url using JSOUP and Coldfusion.

<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>

Solution

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

    The returns an Element representing the <meta> tag. To display only the "content" attribute (which is where that page stores the url), try:

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

    Keep in mind metaOgImage could be null, if it wasn't found, so be sure to add handling for that in the CF code.