How to create PHP Crawler

0 15,083

Hi, we are going to teach you how to create PHP crawler in php mysql to search all links from a particular website containing those links. Web crawler are bots that are used to search for information from websites by scraping their HTML content. crawlers are everywhere they move on and on to many webpages each second. The Google web crawler will enter your domain and scan every page of your website, extracting page titles, descriptions, keywords and links then report back to Google HQ and add information to their huge database. Today i would like to teach you how to make your own basic crawler, not one that scans the whole Internet, though but one that is able to extract all the links from a given website url.

How to create PHP Crawler

Getting Started

You first have to Download the library from the projects website. we will be using helper class Simple HTML DOM. Download this zip file, unzip it, and upload the simple_html_dom.php file to your project folder. it contains function we’ll be using to traverse the elements of a webpage more easily.

Creating A HTML Form to Get URL

in this step we are going to creating a HTML simple form which help we are getting any website url to crawl.

<title>Web crawler</title>
<link type="text/css" rel="stylesheet" href="style.css" />
<form method="post" action="index.php">
<p style="text-align:center;"><input type="text" name="target" class="input" /></p>
<p style="text-align:center;"><input type="submit" name="crawl" class="button" value="Submit" /></p>

Download More Source Code Like

Styling The Form using CSS

in this step we are going to creating some style for form to attractive.

    border: 1px solid red;
    box-sizing: border-box;
    outline: none;
    background-color: red;
    border: none;
    color: white;
    padding:15px 30px 15px 30px;
    text-align: center;
    text-decoration: none;
    font-size: 17px;
    margin: 4px 2px;
    cursor: pointer;

Import Simple_html_dom.php and Some Functions using PHP

$html = new simple_html_dom();
    $crawl = $_POST['target'];
    $find = "http://";
    foreach($html->find('a') as $link)
            echo "<p class='links'>".$link->href."</p>";
        else if(strpos($link,"http://")!==false || strpos($link,"https://")!==false){
            echo "<p class='links'>".$link->href."</p>";
            echo "<p class='links'>"."$crawl/".$link->href."</p>";
        echo "Invalid URL";

If you facing any type of problem with this source code then you can Download the Complete source code in zip Formate by clicking the below button Download Now otherwise you can send Comment.

Download Source Code

Leave A Reply

Your email address will not be published.