PHP code example of arefshojaei / spider

1. Go to this page and download the library: Download arefshojaei/spider library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

arefshojaei / spider example snippets




use Spider\Spider;

$spider = new Spider();

$page = $spider->loadHTML("https://google.com");

echo $page->find("title")->text() . PHP_EOL;

$page->findAll("a")->each(function ($key, $link) {
    echo "[LINK] " . $link->attr("href") . PHP_EOL;
});

$page->find("a");
$page->find(".product");
$page->find("#header");

$page->findAll("a");
$page->findAll(".product");

$page->findAll("a")->each(function ($key, $anchor) {
    echo $anchor->text();
});

$anchors = $page->findAll("a")->map(function ($key, $anchor) {
    $anchor->attr("data-id", rand());

    return $anchor;
});

$links = $page->findAll("a")->filter(
    fn($key, $anchor) => $anchor->attr("href")
);

$parent = $page->find(".product")->parent();

$page->find(".product")
     ->before("<p>Before Element</p>");

$page->find(".product")
     ->after("<p>After Element</p>");

$page->find(".product")
     ->append("<p>New Child</p>");

$page->find(".product")
     ->prepend("<p>First Child</p>");

$page->find("p")->empty();

$page->find("p")->remove();

$text = $page->find("p")->text();

$html = $page->find("p")->html();

$page->find("p")->text("New text");

$page->find("p")->html("<strong>New HTML</strong>");

$attributes = $page->find("a")->attr();

$link = $page->find("a")->attr("href");

$page->find("a")->attr("data-id", 123);

$page->find("p")->addClass("active");

$page->find("p")->removeClass("active");

$page->find("p")->hasClass("active");

$page->find("p")->addID("article");

$page->find("p")->removeID("article");

$page->find("p")->hasID("article");

$filename = "page";

$path = __DIR__ . "/html/" . $filename . rand() . ".html";

$page->export($path);