Scraping elements using XPath (Simple)

Now that we have requested and downloaded a web page, as mentioned in the Making a simple cURL request recipe we can now proceed to scrape the data that we require.

XPath can be used to navigate through elements in an XML document. In this recipe we will convert our downloaded web page into an XML DOM object, from which we will use XPath to scrape the required elements based on their tags and attributes, such as CSS classes and IDs.

How to do it...

  1. Enter the following code into a new PHP project:
    <?php // Function to make GET request using cURL function curlGet($url) { $ch = curl_init(); // Initialising cURL session // Setting cURL options curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, ...

Get Instant PHP Web Scraping now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.