Open In App
Related Articles

Web Scraping in PHP Using Simple HTML DOM Parser

Like Article
Save Article
Report issue

Web Scraping is a technique used to extract large amounts of data from websites extracted and saved them to a local file in your computer or to a database or can be used as API. Data displayed by most websites can be viewed by using a web browser only. They do not offer the functionality to save a copy of this data for use. Thus the only option is to copy and paste the selected data that is required, which in reality, is a very tedious job and may take hours complete. In other terms, Web Scraping is the technique of automating such a process, in place of manual work, the Web Scraping software performs the same task within seconds. 

Web scraping can be done by targeting the selected DOM components and then processing or storing the text between that DOM element of a web page. To do the same in PHP, there is an API that parses the whole page and looks for the required elements within the DOM. It is the Simple HTML DOM Parser. To know more about Web Scraping visit this article. One can download it by clicking this link. 

Example 1: The below-given example shows the use of this API, to display a Google search on the localhost.

HTML Code: 


<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <form action="GoogleSearch.php" method="POST">
        <input type="text" name="search">


PHP code: 


// In case the File is in the API directory
// Extracting DOM
$html = file_get_html(
// Displaying DOM
echo $html;


PHP code: This code will work if you have already searched for anything on the Google Search engine. 


$html = file_get_html(
foreach($html->find('div.kCrYT') as $elements) {
    echo $elements->plaintext;



GeeksforGeeks is a very fast-growing community among programmers
and have a reach of around 10 million+ readers globally. Writing will
surely enhance your knowledge of the subject as before writing any
 topic, you need to be very crisp and clear about it.

Last Updated : 02 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads