Giter Site home page Giter Site logo

crawler's Introduction

Quick example

Consider this HTML to be the $url:

<!DOCTYPE html>
<html>
<head>
    <title>I am a Page Title</title>
</head>
<body>
    <section>
        <h3> I am a row title </h3>

        <ul>
            <li> <a class="anchor -success" href="/redirect/now/1"> Link 1 </a> </li>
            <li> <a class="anchor" href="/redirect/now/2"> Link 2 </a> </li>
        </ul>
    </section>
</body>
</html>
use Napoleon\Crawler\DOMDocument;

$url = 'https://www.example.com';
$document = new DOMDocument($url);

print_r($document->html()->get());

Above code result:

array:1 [
  0 => array:4 [
    "tagName" => "html"
    "attributes" => null
    "content" => ""
    "children" => array:2 [
      0 => array:4 [
        "tagName" => "head"
        "attributes" => null
        "content" => ""
        "children" => array:1 [
          0 => array:4 [
            "tagName" => "title"
            "attributes" => null
            "content" => "I am a Page Title"
            "children" => null
          ]
        ]
      ]
      1 => array:4 [
        "tagName" => "body"
        "attributes" => null
        "content" => ""
        "children" => array:1 [
          0 => array:4 [
            "tagName" => "section"
            "attributes" => null
            "content" => ""
            "children" => array:2 [
              0 => array:4 [
                "tagName" => "h3"
                "attributes" => null
                "content" => "I am a row title"
                "children" => null
              ]
              1 => array:4 [
                "tagName" => "ul"
                "attributes" => null
                "content" => ""
                "children" => array:2 [
                  0 => array:4 [
                    "tagName" => "li"
                    "attributes" => null
                    "content" => ""
                    "children" => array:1 [
                      0 => array:4 [
                        "tagName" => "a"
                        "attributes" => array:2 [
                          "class" => "anchor -success"
                          "href" => "/redirect/now/1"
                        ]
                        "content" => "Link 1"
                        "children" => null
                      ]
                    ]
                  ]
                  1 => array:4 [
                    "tagName" => "li"
                    "attributes" => null
                    "content" => ""
                    "children" => array:1 [
                      0 => array:4 [
                        "tagName" => "a"
                        "attributes" => array:2 [
                          "class" => "anchor"
                          "href" => "/redirect/now/2"
                        ]
                        "content" => "Link 2"
                        "children" => null
                      ]
                    ]
                  ]
                ]
              ]
            ]
          ]
        ]
      ]
    ]
  ]
]
...

Search by specific class name of a tag

Consider this to be https://www.example.com:

<!DOCTYPE html>
<html>
<head>
    <title>I am a Page Title</title>
</head>
<body>
    <section>
        <h3> I am a row title </h3>

        <ul>
            <li> <a class="anchor -success" href="/redirect/now/1"> Link 1 </a> </li>
            <li> <a class="anchor" href="/redirect/now/2"> Link 2 </a> </li>
        </ul>
    </section>
</body>
</html>
use Napoleon\Crawler\DOMDocument;

$url = 'https://www.example.com';
$document = new DOMDocument($url);
print_r($document->findByClass('anchor')->get());

The result above:

.array:2 [
  0 => array:3 [
    "tagName" => "a"
    "attributes" => array:2 [
      "class" => "anchor -success"
      "href" => "/redirect/now/1"
    ],
    "content" => "Link 1",
    "children" => null
  ]
  1 => array:3 [
    "tagName" => "a"
    "attributes" => array:2 [
      "class" => "anchor"
      "href" => "/redirect/now/2"
    ],
    "content" => "Link 2",
    "children" => null
  ]
]

crawler's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.