7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
  1. Code
  2. PHP

Scrape Search Engine Result Pages in Real Time With Zenserp

Scroll to top
Read Time: 10 mins
Sponsored Content

This sponsored post features a product relevant to our readers while meeting our editorial guidelines for being objective and educational.

In one of my previous posts, I talked about using Zenscrape to extract data at scale. Their API makes it incredibly easy for you to make a lot of requests to scrape webpages from different websites. Zenscrape took care of a lot of things for us, like localization and JavaScript rendering. All this makes it possible for you to extract content from any webpage that you need.

What if you want to scrape search engine result pages? You can certainly do that with Zenscrape, but there is a more specialized service created by the same team of developers called Zenserp that will help you do it more efficiently.

Advantages of Using Zenserp

I will begin the tutorial by listing some of the advantages offered by Zenserp when it comes to extracting real-time data from search engine result pages.

Coverage of All Available Snippets

I am sure you have noticed during your own Google searches that not all search pages show the same kinds of search results. Users get a mixed set of content, depending on the search terms they used.

Besides including regular organic search results, these search engine result pages (SERPs) can contain snippets for other types of content such as news results, paid results including all kinds of Google ads, related questions, knowledge graphs, and some image or video results.

Filtering through all that content to find what you are looking for can be difficult. However, Zenserp takes care of sorting through all this data for you automatically. It also regularly updates the API to include any new type of content added to the SERPs.

Support for Multiple Platforms

It is no secret that Google dominates the search engine market. Keeping this in mind, the Zenserp API provides multiple endpoints specific to Google-based search results. For example, you can send a request for a regular SERP in Google or you can be more specific and get search results from the Images, Videos, Maps, News, or Shopping tabs.

The Zenserp API also has endpoints to give you search results from YouTube, Bing, Yandex and DuckDuckGo. They also intend to keep adding new endpoints to the API when the need arises.

These and many other features help you save valuable time to focus on your core business idea instead of spending time figuring out how to extract information from SERPs.

Scraping SERPs Using the Zenserp API

Now, it's time to learn how we can take advantage of the Zenserp API to extract valuable SERP data in real time.

You should start by creating a free account on Zenserp. Signing up for a free plan will give you a quota of 50 API requests per month. This will make it easier for you to follow this tutorial while allowing you to run your own tests with the API. Zenserp also personalizes the code snippets in the documentation by filling in your API keys once you have logged in successfully.

Zenserp Registration NoticeZenserp Registration NoticeZenserp Registration Notice

The API keys are also useful for executing all your requests in the Zenserp playground. You can simply select the API endpoint from the dropdown and fill out any other blank fields that you like before executing the request.

Zenserp API PlaygroundZenserp API PlaygroundZenserp API Playground

Getting SERP Data From a Regular Google Search

Let's begin with one of the most common search scenarios: people type something in the search bar and get the search results.

You can get the search results by making a request to /api/v2/search in the API with the appropriate parameters. We will use the search term "Gift Ideas" in our case. As I mentioned earlier, you can either run this request through the Zenserp playground or get back some data from the API endpoint programmatically. Both these will cost you one credit each.

The only required query parameter here is the search term, and it is denoted by q. You are also likely to get different search results depending on your location and the type of device you are using. However, those are optional parameters.

The following PHP code with the Zenserp API will help us get Google SERP data as if we were running a search query from New York. Please note that we have decoded the response as an associative array using the json_decode() function to navigate through key-value pairs.

Zenserp also shows you what the SERP for the above request actually looks like. You can see that the JSON text response included below matches whatever we see visually.

Zenserp Visual Represenation of JSON ResponseZenserp Visual Represenation of JSON ResponseZenserp Visual Represenation of JSON Response

Here is part of the JSON response that we get back while executing the above API request. We can now write some code to extract the data we need by taking advantage of this neatly structured JSON.

Let's say we want to know the questions that people usually ask when they are looking for "Gift Ideas". This means that we will be searching for the questions key in the organic results. We can then loop through the organic results, assuming that we don't know where the questions will be located and don't want to provide a hard-coded value.

Here's the code that will give us a list of all the "People also ask" questions:

Using More Specific Google Search Endpoints

As I mentioned earlier, the Zenserp API also provides some more specific endpoints if you want to find just images, videos, shopping results, etc., for a particular query. This is done with the help of the tbm parameter. It can have one of the following values:

Parameter Value Search Type
isch Image Search
vid Video Search
nws News Search
shop Shopping Search

Since we are interested in limiting our search to image results, I will set it to isch for our example. The search query this time will be "Apollo 11". Here is the PHP code to execute the Zenserp API request and get the image search data.

As you can see, we didn't have to change much to turn our regular Google search query into an image search for Zenserp. All we did was add a tbm parameter and update the query parameter q.

The JSON response that you get back from the API will look similar to the one below.

We can use the following PHP code to iterate through all the image results returned by Zenserp and easily extract the required information from the data. In our case, we just want the sourceUrl, title, and link.

You can use it to do many things, like downloading all those images or determining how many of the top 100 images are shown from your or some other website. For this example, we output all the information as part of figure tags on a webpage.

Zenserp Google Image Search ResultsZenserp Google Image Search ResultsZenserp Google Image Search Results

Get Bing SERP Data Using Zenserp

The Zenserp API also has endpoints to help you get data from the search results of other popular search engines like Bing, Yandex, and DuckDuckGo. The code to get back a JSON response for a query is very similar to what we have been doing so far.

The PHP code below will give you the search results for the query "Mangoes" on Bing. I should mention that Zenserp generates all these snippets for you automatically in its playground.

Running the above code will give you a JSON response that looks similar to the one below.

Just like the Google SERP in our first example, Zenserp provides a visual representation of the results for direct comparison.

Zenscrape Bing SERP VisualZenscrape Bing SERP VisualZenscrape Bing SERP Visual

We can now extract whatever information we like from the JSON response. In this example, I chose to create a list of organic search results as well as the related searches. Here is my code to do that.

The following image shows the end result after using a little bit of CSS.

Bing SERP Data Extracted and FormattedBing SERP Data Extracted and FormattedBing SERP Data Extracted and Formatted

Final Thoughts

Zenserp is a great tool for extracting useful information from the result pages of different search engines. If you have ever tried to create a tool to scrape information from SERPs, you will start to truly appreciate the amount of time and effort it saves by giving access to all that data with a very simple API.

You no longer need to worry about creating sophisticated scrapers and then constantly updating your parser for the ever-changing markup of those search result pages to get back some useful data. The best part is that you don't have to pay anything upfront to test Zenserp. You can create a free account in a couple of minutes and get 50 API requests free each month to see if it meets your project needs.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.