Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. PHP

How to Scrape Web Pages for Metadata

This post is part of a series called How to Scrape Web Pages for Metadata.
Using the New York Times API to Scrape Metadata
Final product image
What You'll Be Creating


Recently I began building a community site on top of the Yii platform, which I'll write about soon as part of my Programming With Yii2 series. I wanted to make it easy to add links related to content on the site. While it's easy for people to paste URLs into forms, it becomes time-consuming to also provide title and source information.

In today's tutorial, I'm going to show you how to leverage PHP to scrape common metadata from web pages to make it easier for your users to participate and to build more interesting services.

Remember, I participate in the comment threads below, so tell me what you think! You can also reach me on Twitter @lookahead_io

Getting Started

First, I built a form for people to add links by pasting the URL. I also created a Lookup button to use AJAX to request the web page be scraped for metadata information.

Web Scraping - Create Link form with Lookup button

Pressing Lookup calls the Link::grab() function via ajax:

Scraping the Page

The Link::grab() code calls fetch_og(). This imitates a crawler to capture the page and get the metadata with DOMXPath:

For my scenario, I've replaced the og: tags above, but the code below looks for various types of tags:

You can also grab other tags such as keywords, description, etc. The jQuery then adds the result to the form for the user to submit:

Web Scraping - Create Link with Resulting Title Filled In

Going Further

I also have a table of sources which I'll develop more later. But basically, each time a new URL is added, we parse it for the base website domain and place it in a Source table:

For now, I'm manually updating the names of sources so they'll look clean to the user, e.g. ABC News, BoingBoing, and Vice:

Web Scraping - Item page with completed links and clean sources

Hopefully, in an upcoming episode, I'll review how to use freely available APIs to look up the site's name. It's odd to me there's no common metatag for this; if only the Internet were perfect.

Paywall Sites

Some sites like The New York Times do not let you scrape the metadata because of their paywalls. But they do have an API. It's not easy to learn because of the confusing documentation, but their developers are quick to help on GitHub. I also hope to write about using the metadata lookup for New York Times titles in a future episode.

In Closing

I hope you found this scraping guide helpful and put it to use somewhere in your projects. If you'd like to see it in action, you can try out some of the web scraping on my site, Active Together.

Please do share any thoughts and feedback in the comments. You can also always reach me on Twitter @lookahead_io directly. And be sure to check out my instructor page and my other series, Building Your Startup With PHP and Programming With Yii2.

Related Links

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.