Easy page scraping with Zend\Dom (from Zend Framework 2)
Let me assure you, though, that by using Zend\Dom from the Zend Framework 2 library it’s actually a really simple task. In fact, I did it in around 20 lines of code.
So let’s start by looking at the code and then break it down a little more.
The first thing you’ll notice is that, OK, it’s not just 20 lines. I’ve added a little caching function to be kind to the academia.edu servers if I want to test out new selectors and so on, but the code functionality starts at line 31. But that’s getting ahead of ourselves, so let’s break it down.
First you’ll want to include your autoloader however you’re doing it, be it with Composer, setting it up yourself, or whatever, and include the required namespace classes (here I’ve also included Zend\Debug with the Zend\Dom just for easy output).
This just caches the result of the file_get_contents fetch so that it can be used again and it also ensures that the contents are stored in UTF-8. On subsequent calls with the same url it’ll return the cached results rather than connect to the remote website to get the contents. But you know that – I don’t really need to teach you what caching is all about!
Get each page
We start off by getting the initial page and instantiate Zend\Dom\Query using the page contents. The power of this class is really in its simplicity of use, which is basically passing it a selector path. If you’ve ever used jQuery or Zepto or something like that you’ll be very familiar with how handy and powerful selectors can be to get down the dom tree to the elements you want. Zend\Dom does the same thing, only in PHP.
So the code above uses the query parser to get all the anchor tags that are in unordered list elements that are contain within a div with the id of department_list. The execute method returns a NodeList which implements both Iterator and ArrayAccess, so you can loop through the results very easily. And from each anchor we can get the href property which gives us the page that contains all of the people and their tags.
With each page found we load up the contents and create another Query object and from there we can execute a query to look for the h1 tag which contains the title text of the page we’re currently processing, so we want to get that for later.
If you’ve ever used selectors in jQuery you may be aware that you can look for selectors within the scope of another selector – basically you can think of that as doing a sub-query in a chink of html within your page. I found that the easiest way to to this with Zend\Dom was to convert the node that was found from my previous query execute into SimpleXML and then output that out as XML and pass it right back to a new instance of the Query class. All of that takes place on lines 36 and 38, but as I had the SimpleXML object I also used it to make sure the fieldset my previous execute had found had the legend of ‘faculty’.
All of this should be pretty familiar by now… It’s just looping through the results of a selector, doing a sub-query with the SimpleXML trick, and then adding the results to an array.
So starting with a page that only contains links to other pages you’d end up with an array that looks like this: