O'Reilly logo

Instant PHP Web Scraping by Jacob Ward

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required