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

Traversing multiple pages (Intermediate)

Up to this point, everything we have worked with has been scraping content from a single page, but where scraping really provides the most valuable results is in crawling and scraping multiple pages.

In the following recipe we will access the paginated result of a search query, visit each page of search results, and scrape all of the results.

How to do it...

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

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