Apify users sometimes need to extract data from websites that use schema.org Microdata attributes to describe the meaning of their HTML content. Clearly, there must be some simple way to extract structured data from such HTML purely using these schema.org tags. Well, look no further. Simply use the following JavaScript code for your page function to automatically extract the data:

function pageFunction(context) {    var extractValue = function(elem) {
        return $(elem).attr("content") || $(elem).text()
               || $(elem).attr("src") || $(elem).attr("href") || null;
    };
    var addProperty = function(item,propName,value) {
        if( typeof(value)==='string' )
            value = value.trim();
        if( Array.isArray(item[propName]) )
            item[propName].push(value);
        else if( typeof(item[propName])!=='undefined' )
            item[propName] = [item[propName], value];
        else
            item[propName] = value;
    }
    var extractItem = function(elem) {
        var item = { _type: $(elem).attr("itemtype") };
        var count = 0;
        // iterate itemprops not nested in another itemscope    
        $(elem).find("[itemprop]").filter(function() {
            return $(this).parentsUntil(elem, '[itemscope]').length === 0;
        }).each( function() {
            addProperty(
                item,
                $(this).attr("itemprop"),
                $(this).is("[itemscope]") ? extractItem(this) : extractValue(this));
            count++;
        });
        // special case - output at least something
        if( count===0 )
            addProperty(item, "_value", extractValue(elem));
        return item;
    };
    var extractAllItems = function() {
        var items = [];
        // find top-level itemscope elements
        $("[itemscope]").filter(function() {
            return $(this).parentsUntil("body", '[itemscope]').length === 0;
        }).each( function() {
            items.push( extractItem(this) );
        });
        return items;
    };    return extractAllItems();
}

For example, when crawling the IMDB entry for the movie Inception, the page function will give you the following results:

[{
  "_type": "http://schema.org/Movie",
  "aggregateRating": {
    "_type": "http://schema.org/AggregateRating",
    "ratingValue": "8.8",
    "bestRating": "10",
    "ratingCount": "1,507,540",
    "reviewCount": [
      "2,825 user",
      "647 critic"
    ]
  },
  "name": "Inception (2010)",
  "contentRating": [
    "PG-13",
    "Rated PG-13 for sequences of violence and action throughout"
  ],
  "duration": [
    "2h 28min",
    "148 min"
  ],
  "genre": [
    "Action",
    "Adventure",
    "Sci-Fi",
    "Genres:\n Action |\n Adventure |\n Sci-Fi |\n Thriller"
  ],
  "datePublished": [
    "2010-07-16",
    "17 November 2016 5:09 PM, -05:00",
    "14 November 2016 4:03 PM, -05:00",
    "14 November 2016 9:46 AM, -05:00"
  ],
  "image": [
    "https://images-na.ssl-images-amazon.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_UX182_CR0,0,182,268_AL_.jpg",
    "http://ia.media-imdb.com/images/M/MV5BMjI2NDI3NTA4M15BMl5BanBnXkFtZTgwMDQ5NjYzMzE@._V1_CR167,0,946,532_AL_UY268_CR84,0,477,268_AL_.jpg",
    "https://images-na.ssl-images-amazon.com/images/M/MV5BMDAzNDAxOGItNWFlMC00NjE1LTk2ZjctYzhiYTEzMjYxNzdiXkEyXkFqcGdeQXVyNTY0MTkxMTg@._CR241,39,1153,864_UX614_UY460._SY230_SX307_AL_.jpg",
    "https://images-na.ssl-images-amazon.com/images/G/01/imdb/images/widget/amazon._CB339202444_.png",
    "https://images-na.ssl-images-amazon.com/images/M/MV5BMTM5MjMyMTgxNl5BMl5BanBnXkFtZTcwOTM0OTk1Mw@@._V1_UY105_CR26,0,105,105_AL_.jpg",
    "https://images-na.ssl-images-amazon.com/images/M/MV5BMTM0MjUzNjkwMl5BMl5BanBnXkFtZTcwNjY0OTk1Mw@@._V1_UX105_CR0,0,105,105_AL_.jpg",
    "https://images-na.ssl-images-amazon.com/images/M/MV5BMjExMjkwNTQ0Nl5BMl5BanBnXkFtZTcwNTY0OTk1Mw@@._V1_UX105_CR0,0,105,105_AL_.jpg"
  ],
  "trailer": "",
  "description": [
    "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
    "Dom Cobb is a skilled thief, the absolute best in the dangerous art of extraction, stealing valuable secrets from deep within the subconscious during the dream state, when the mind is at its most vulnerable. Cobb's rare ability has made him a coveted player in this treacherous new world of corporate espionage, but it has also made him an international fugitive and cost him everything he has ever loved. Now Cobb is being offered a chance at redemption. One last job could give him his life back but only if he can accomplish the impossible - inception. Instead of the perfect heist, Cobb and his team of specialists have to pull off the reverse: their task is not to steal an idea but to plant one. If they succeed, it could be the perfect crime. But no amount of careful planning or expertise can prepare the team for the dangerous enemy that seems to predict their every move. An enemy that only Cobb could have seen coming.                Written by\nWarner Bros. Pictures"
  ],
  "director": {
    "_type": "http://schema.org/Person",
    "url": "Christopher Nolan",
    "name": "Christopher Nolan"
  },
  "creator": [
    {
      "_type": "http://schema.org/Person",
      "url": "Christopher Nolan",
      "name": "Christopher Nolan"
    },
    {
      "_type": "http://schema.org/Organization",
      "url": "Warner Bros.",
      "name": "Warner Bros."
    },
    {
      "_type": "http://schema.org/Organization",
      "url": "Legendary Entertainment",
      "name": "Legendary Entertainment"
    },
    {
      "_type": "http://schema.org/Organization",
      "url": "Syncopy",
      "name": "Syncopy"
    }
  ],
  "actors": [
    {
      "_type": "http://schema.org/Person",
      "url": "Leonardo DiCaprio",
      "name": "Leonardo DiCaprio"
    },
    {
      "_type": "http://schema.org/Person",
      "url": "Joseph Gordon-Levitt",
      "name": "Joseph Gordon-Levitt"
    },
    {
      "_type": "http://schema.org/Person",
      "url": "Ellen Page",
      "name": "Ellen Page"
    }
  ],
  "headline": [
    "Why Eddie Redmayne Isn’t Worried That ‘Fantastic Beasts’ Expanded to Five Films",
    "‘Doctor Strange’: How VFX Tapped ‘Inception’ for a New Marvel Dimension",
    "The Crown review: a mesh of history books and melodrama"
  ],
  "provider": [
    "Variety - Film News",
    "Indiewire",
    "Den of Geek"
  ],
  "url": [
    "Show HTML",
    "View more styles",
    "Official Facebook",
    "Warner Bros. [Germany]",
    "See more",
    "USA",
    "UK",
    "English",
    "Japanese",
    "French",
    "See more",
    "See more",
    "Bedfordshire, England, UK",
    "See more",
    "See more",
    "See more",
    "company contact information",
    "IMDbPro",
    "Dolby Digital",
    "DTS",
    "SDDS",
    "Color",
    "full technical specs"
  ],
  "awards": [
    "Won\n        4\n        Oscars.",
    "Another\n      144 wins & 198 nominations."
  ],
  "thumbnailUrl": [
    "/title/tt1375666/mediaviewer/rm3636364032?context=default&ref_=tt_pv_md_1",
    "/title/tt1375666/mediaviewer/rm918520576?context=default&ref_=tt_pv_md_2",
    "/title/tt1375666/mediaviewer/rm935297792?context=default&ref_=tt_pv_md_3"
  ],
  "actor": [
    {
      "_type": "http://schema.org/Person",
      "url": "Leonardo DiCaprio",
      "name": "Leonardo DiCaprio"
    },
    {
      "_type": "http://schema.org/Person",
      "url": "Joseph Gordon-Levitt",
      "name": "Joseph Gordon-Levitt"
    },
    {
      "_type": "http://schema.org/Person",
      "url": "Ellen Page",
      "name": "Ellen Page"
    },
    {
      "_type": "http://schema.org/Person",
      "url": "Tom Hardy",
      "name": "Tom Hardy"
    },
    {
      "_type": "http://schema.org/Person",
      "url": "Ken Watanabe",
      "name": "Ken Watanabe"
    },
    {
      "_type": "http://schema.org/Person",
      "url": "Dileep Rao",
      "name": "Dileep Rao"
    },
    {
      "_type": "http://schema.org/Person",
      "url": "Cillian Murphy",
      "name": "Cillian Murphy"
    },
    {
      "_type": "http://schema.org/Person",
      "url": "Tom Berenger",
      "name": "Tom Berenger"
    },
    {
      "_type": "http://schema.org/Person",
      "url": "Marion Cotillard",
      "name": "Marion Cotillard"
    },
    {
      "_type": "http://schema.org/Person",
      "url": "Pete Postlethwaite",
      "name": "Pete Postlethwaite"
    },
    {
      "_type": "http://schema.org/Person",
      "url": "Michael Caine",
      "name": "Michael Caine"
    },
    {
      "_type": "http://schema.org/Person",
      "url": "Lukas Haas",
      "name": "Lukas Haas"
    },
    {
      "_type": "http://schema.org/Person",
      "url": "Tai-Li Lee",
      "name": "Tai-Li Lee"
    },
    {
      "_type": "http://schema.org/Person",
      "url": "Claire Geare",
      "name": "Claire Geare"
    },
    {
      "_type": "http://schema.org/Person",
      "url": "Magnus Nolan",
      "name": "Magnus Nolan"
    }
  ],
  "keywords": [
    "Plot Keywords:\n dream\n                        |\n subconscious\n                        |\n ambiguous ending\n                        |\n thief\n                        |\n architecture\n            | See All (283) »",
    "dream",
    "subconscious",
    "ambiguous ending",
    "thief",
    "architecture"
  ],
  "audience": {
    "_type": "http://schema.org/Audience",
    "url": "View content advisory"
  },
  "review": {
    "_type": "http://schema.org/Review",
    "name": "Sci-fi perfection. A truly mesmerizing film.",
    "reviewRating": {
      "_type": "http://schema.org/Rating",
      "worstRating": "1",
      "ratingValue": "8",
      "bestRating": "10"
    },
    "author": "dvc5159",
    "datePublished": "2010-07-15",
    "reviewBody": "I'm nearly at a loss for words. Just when you thought Christopher Nolan couldn't follow up to \"The Dark Knight\", he does it again, delivering another masterpiece, one with so much power and rich themes that has been lost from the box office for several years. Questioning illusions vs reality usually makes the film weird, but Nolan grips your attention like an iron claw that you just can't help watching and wondering what will happen next. That is a real powerful skill a director has. No wonder Warner Bros. put their trust in him, he is THAT good of a director, and over-hyping a Christopher Nolan film, no matter what the film is about, is always an understatement instead of an overestimate like MANY films before.Not since the eras of Stanley Kubrick, Andrei Tarkovsky and Alfred Hitchcock has there been a more brilliant director than Christopher Nolan. He is, undoubtedly, one of THE most brilliant and gifted Hollywood filmmakers in history. Filmmakers like him come but just once in a lifetime. He has the ability to seduce our eyes, ears and most importantly, mind, and then delivers what he intends to deliver in full blast. Rarely have blockbusters have the gall to deliver such amounts imagination and intelligence at the same time. And yes, it is similar to the excellent anime film \"Paprika\" in the whole \"invading dreams\" plot, but the similarities end there as Nolan brings the film to a whole different level.Visuals and intelligence rarely come together in movies at the same time, it's either all-visuals-no-smarts (\"G.I. Joe\", \"Transformers\") or the exact opposite (\"Doubt\", \"Invictus\"). In this film the excellently directed action sequences combined with immensely groundbreaking and jaw-dropping visual effects are combined smoothly with a heavy dose of intelligence and believability.Although having an ensemble cast and financed by a Hollywood giant studio (Warner Bros), this film is a very personal film for Nolan, he wrote the film as well as directing it, and as you watch the film you get many glimpses of Nolan's perplexing, increasingly imaginative thoughts and dreams in the dialog that he writes and the plot that he sets up. Ideas have never felt more interesting and put to good use than in this film. This film is NOT for the popcorn muncher, rather it is a film for thinkers. Honestly I can't explain the plot for fear of spoiling the movie for you readers. Even the slightest hint will ruin the experience. The viewer will walk out of the cinema feeling dazed, confused and ultimately breathless. It's like a puzzle, both physically and mentally, and you have to pay attention throughout the film for the clues. However Nolan controls the spectacle of the film and is careful not to let it overwhelm the film's humanity, and this is where \"Inception\" shines. It is a very deep film that will have one thinking and asking questions for years to come. That's right, years.Once the film ends, you'll want to watch it again, for there's something new every time. This is a film that requires multiple viewing for someone to truly comprehend the film's ambiguous themes, and will be discussed by many in the future. This is an original film, no adaptation, no sequel/prequel, no remake/reboot, which is extremely refreshing having gone nearly three years of mostly unremarkable visual effects roadshows (\"Avatar\" be damned).Of course, a film is not complete without the actors. Leo DiCaprio delivers an Oscar worthy performance, similar but better than his previous effort \"Shutter Island\". He shows glimpses of a flawed, grim, fragile man, who has knowledge about everything else but yet can't seem to come in grips with himself and his demons. Joseph Gordon-Levitt, Ellen Page, Tom Hardy, Ken Watanabe, Dileep Rao, Cillian Murphy, Tom Berenger, Michael Caine, Marion Cottilard, Pete Postlethwaite, Lukas Haas... Nolan really brings out the best in this unusual yet extremely talented group of supporting actors who make their roles their own.Nolan is of course, a master behind the camera, a real virtuoso when it comes to film. His direction is taut, focused, gripping, and extraordinarily fascinating. The detailed and complex original script is no surprise from Nolan, considering the fact that he turned Batman the superhero into pop-culture art two years ago (\"The Dark Knight\"). The action sequences are unique, exciting and fresh, something absent from the cinema which has since been interested at things popping towards the screen and stuff blowing up every two milliseconds. The visual effects are awesome and imaginative, and best of all they do not bring down the movie's quality one bit, rather it makes the movie more fascinating to watch. The cinematography is absolutely, beautifully shot, so we can see the action and emotion in all their glory. Production design is top notch, with terrific design of sets and locations. Hans Zimmer's complementing music score is simply outstanding, and knowing the man, that's really all I have to say. Together all of these elements combine to deliver a mesmerizing movie experience like no other this year.Christopher Nolan has once again outdone himself. He truly is a gifted filmmaker, arguably the most imaginative in Hollywood today. And \"Inception\" can proudly stand alongside \"Blade Runner\" and \"2001: A Space Odyssey\" as science-fiction masterpieces that push the boundaries for movie making and become a different experience altogether.There are two types of films: the crowd-pleasing blockbuster and the intelligent indie/art film. Nolan has combined two of these tropes together into one exceptionally brilliant package, pulling off that rare, now nearly-extinct Movie Magic that has since been wiped off the planet by sequelitis and reboots. Movie of Summer 2010? Movie of 2010? Heck this is possibly the first masterpiece of the decade! Nolan is a genius and I applaud him for treating his audiences as intelligent people.Missing this film and not getting the film's point is a crime.Overall value: 9/10 (Excellent)"
  }
}]
Did this answer your question?