The DomCrawler Component

The DomCrawler Component

The DomCrawler component eases DOM navigation for HTML and XML documents.

Note

While possible, the DomCrawler component is not designed for manipulation of the DOM or re-dumping HTML/XML.

Installation

  1. $ composer require symfony/dom-crawler

Note

If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.

Usage

See also

This article explains how to use the DomCrawler features as an independent component in any PHP application. Read the Symfony Functional Tests article to learn about how to use it when creating Symfony tests.

The Symfony\Component\DomCrawler\Crawler class provides methods to query and manipulate HTML and XML documents.

An instance of the Crawler represents a set of DOMElement objects, which are nodes that can be traversed as follows:

  1. use Symfony\Component\DomCrawler\Crawler;
  2. $html = <<<'HTML'
  3. <!DOCTYPE html>
  4. <html>
  5. <body>
  6. <p class="message">Hello World!</p>
  7. <p>Hello Crawler!</p>
  8. </body>
  9. </html>
  10. HTML;
  11. $crawler = new Crawler($html);
  12. foreach ($crawler as $domElement) {
  13. var_dump($domElement->nodeName);
  14. }

Specialized Symfony\Component\DomCrawler\Link, Symfony\Component\DomCrawler\Image and Symfony\Component\DomCrawler\Form classes are useful for interacting with html links, images and forms as you traverse through the HTML tree.

Note

The DomCrawler will attempt to automatically fix your HTML to match the official specification. For example, if you nest a <p> tag inside another <p> tag, it will be moved to be a sibling of the parent tag. This is expected and is part of the HTML5 spec. But if you’re getting unexpected behavior, this could be a cause. And while the DomCrawler isn’t meant to dump content, you can see the “fixed” version of your HTML by dumping it.

Note

If you need better support for HTML5 contents or want to get rid of the inconsistencies of PHP’s DOM extension, install the html5-php library. The DomCrawler component will use it automatically when the content has an HTML5 doctype.

New in version 4.3: The automatic support of the html5-php library was introduced in Symfony 4.3.

Node Filtering

Using XPath expressions, you can select specific nodes within the document:

  1. $crawler = $crawler->filterXPath('descendant-or-self::body/p');

Tip

DOMXPath::query is used internally to actually perform an XPath query.

If you prefer CSS selectors over XPath, install The CssSelector Component. It allows you to use jQuery-like selectors:

  1. $crawler = $crawler->filter('body > p');

An anonymous function can be used to filter with more complex criteria:

  1. use Symfony\Component\DomCrawler\Crawler;
  2. // ...
  3. $crawler = $crawler
  4. ->filter('body > p')
  5. ->reduce(function (Crawler $node, $i) {
  6. // filters every other node
  7. return ($i % 2) == 0;
  8. });

To remove a node, the anonymous function must return false.

Note

All filter methods return a new Symfony\Component\DomCrawler\Crawler instance with the filtered content. To check if the filter actually found something, use $crawler->count() > 0 on this new crawler.

Both the filterXPath() and filter() methods work with XML namespaces, which can be either automatically discovered or registered explicitly.

Consider the XML below:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <entry
  3. xmlns="http://www.w3.org/2005/Atom"
  4. xmlns:media="http://search.yahoo.com/mrss/"
  5. xmlns:yt="http://gdata.youtube.com/schemas/2007"
  6. >
  7. <id>tag:youtube.com,2008:video:kgZRZmEc9j4</id>
  8. <yt:accessControl action="comment" permission="allowed"/>
  9. <yt:accessControl action="videoRespond" permission="moderated"/>
  10. <media:group>
  11. <media:title type="plain">Chordates - CrashCourse Biology #24</media:title>
  12. <yt:aspectRatio>widescreen</yt:aspectRatio>
  13. </media:group>
  14. </entry>

This can be filtered with the Crawler without needing to register namespace aliases both with filterXPath():

  1. $crawler = $crawler->filterXPath('//default:entry/media:group//yt:aspectRatio');

and filter():

  1. $crawler = $crawler->filter('default|entry media|group yt|aspectRatio');

Note

The default namespace is registered with a prefix “default”. It can be changed with the setDefaultNamespacePrefix() method.

The default namespace is removed when loading the content if it’s the only namespace in the document. It’s done to simplify the XPath queries.

Namespaces can be explicitly registered with the registerNamespace() method:

  1. $crawler->registerNamespace('m', 'http://search.yahoo.com/mrss/');
  2. $crawler = $crawler->filterXPath('//m:group//yt:aspectRatio');

Verify if the current node matches a selector:

  1. $crawler->matches('p.lorem');

New in version 4.4: The `matches() method was introduced in Symfony 4.4.

Node Traversing

Access node by its position on the list:

  1. $crawler->filter('body > p')->eq(0);

Get the first or last node of the current selection:

  1. $crawler->filter('body > p')->first();
  2. $crawler->filter('body > p')->last();

Get the nodes of the same level as the current selection:

  1. $crawler->filter('body > p')->siblings();

Get the same level nodes after or before the current selection:

  1. $crawler->filter('body > p')->nextAll();
  2. $crawler->filter('body > p')->previousAll();

Get all the child or parent nodes:

  1. $crawler->filter('body')->children();
  2. $crawler->filter('body > p')->parents();

Get all the direct child nodes matching a CSS selector:

  1. $crawler->filter('body')->children('p.lorem');

Get the first parent (heading toward the document root) of the element that matches the provided selector:

  1. $crawler->closest('p.lorem');

New in version 4.4: The `closest() method was introduced in Symfony 4.4.

Note

All the traversal methods return a new Symfony\Component\DomCrawler\Crawler instance.

Accessing Node Values

Access the node name (HTML tag name) of the first node of the current selection (e.g. “p” or “div”):

  1. // returns the node name (HTML tag name) of the first child element under <body>
  2. $tag = $crawler->filterXPath('//body/*')->nodeName();

Access the value of the first node of the current selection:

  1. // if the node does not exist, calling to text() will result in an exception
  2. $message = $crawler->filterXPath('//body/p')->text();
  3. // avoid the exception passing an argument that text() returns when node does not exist
  4. $message = $crawler->filterXPath('//body/p')->text('Default text content');
  5. // pass TRUE as the second argument of text() to remove all extra white spaces, including
  6. // the internal ones (e.g. " foo\n bar baz \n " is returned as "foo bar baz")
  7. $crawler->filterXPath('//body/p')->text('Default text content', true);

New in version 4.3: The default argument of `text() was introduced in Symfony 4.3.

New in version 4.4: The option to trim white spaces in `text() was introduced in Symfony 4.4.

Access the attribute value of the first node of the current selection:

  1. $class = $crawler->filterXPath('//body/p')->attr('class');

Extract attribute and/or node values from the list of nodes:

  1. $attributes = $crawler
  2. ->filterXpath('//body/p')
  3. ->extract(['_name', '_text', 'class'])
  4. ;

Note

Special attribute _text represents a node value, while _name represents the element name (the HTML tag name).

New in version 4.3: The special attribute _name was introduced in Symfony 4.3.

Call an anonymous function on each node of the list:

  1. use Symfony\Component\DomCrawler\Crawler;
  2. // ...
  3. $nodeValues = $crawler->filter('p')->each(function (Crawler $node, $i) {
  4. return $node->text();
  5. });

The anonymous function receives the node (as a Crawler) and the position as arguments. The result is an array of values returned by the anonymous function calls.

When using nested crawler, beware that `filterXPath() is evaluated in the context of the crawler:

  1. $crawler->filterXPath('parent')->each(function (Crawler $parentCrawler, $i) {
  2. // DON'T DO THIS: direct child can not be found
  3. $subCrawler = $parentCrawler->filterXPath('sub-tag/sub-child-tag');
  4. // DO THIS: specify the parent tag too
  5. $subCrawler = $parentCrawler->filterXPath('parent/sub-tag/sub-child-tag');
  6. $subCrawler = $parentCrawler->filterXPath('node()/sub-tag/sub-child-tag');
  7. });

Adding the Content

The crawler supports multiple ways of adding the content, but they are mutually exclusive, so you can only use one of them to add content (e.g. if you pass the content to the Crawler constructor, you can’t call `addContent() later):

  1. $crawler = new Crawler('<html><body/></html>');
  2. $crawler->addHtmlContent('<html><body/></html>');
  3. $crawler->addXmlContent('<root><node/></root>');
  4. $crawler->addContent('<html><body/></html>');
  5. $crawler->addContent('<root><node/></root>', 'text/xml');
  6. $crawler->add('<html><body/></html>');
  7. $crawler->add('<root><node/></root>');

Note

The addHtmlContent() and addXmlContent() methods default to UTF-8 encoding but you can change this behavior with their second optional argument.

The addContent() method guesses the best charset according to the given contents and defaults to ISO-8859-1 in case no charset can be guessed.

As the Crawler’s implementation is based on the DOM extension, it is also able to interact with native DOMDocument, DOMNodeList and DOMNode objects:

  1. $domDocument = new \DOMDocument();
  2. $domDocument->loadXml('<root><node/><node/></root>');
  3. $nodeList = $domDocument->getElementsByTagName('node');
  4. $node = $domDocument->getElementsByTagName('node')->item(0);
  5. $crawler->addDocument($domDocument);
  6. $crawler->addNodeList($nodeList);
  7. $crawler->addNodes([$node]);
  8. $crawler->addNode($node);
  9. $crawler->add($domDocument);

Manipulating and Dumping a Crawler

These methods on the Crawler are intended to initially populate your Crawler and aren’t intended to be used to further manipulate a DOM (though this is possible). However, since the Crawler is a set of DOMElement objects, you can use any method or property available on DOMElement, DOMNode or DOMDocument. For example, you could get the HTML of a Crawler with something like this:

  1. $html = '';
  2. foreach ($crawler as $domElement) {
  3. $html .= $domElement->ownerDocument->saveHTML($domElement);
  4. }

Or you can get the HTML of the first node using html():

  1. // if the node does not exist, calling to html() will result in an exception
  2. $html = $crawler->html();
  3. // avoid the exception passing an argument that html() returns when node does not exist
  4. $html = $crawler->html('Default <strong>HTML</strong> content');

New in version 4.3: The default argument of `html() was introduced in Symfony 4.3.

Or you can get the outer HTML of the first node using outerHtml():

  1. $html = $crawler->outerHtml();

New in version 4.4: The `outerHtml() method was introduced in Symfony 4.4.

Expression Evaluation

The evaluate() method evaluates the given XPath expression. The return value depends on the XPath expression. If the expression evaluates to a scalar value (e.g. HTML attributes), an array of results will be returned. If the expression evaluates to a DOM document, a newCrawler` instance will be returned.

This behavior is best illustrated with examples:

  1. use Symfony\Component\DomCrawler\Crawler;
  2. $html = '<html>
  3. <body>
  4. <span id="article-100" class="article">Article 1</span>
  5. <span id="article-101" class="article">Article 2</span>
  6. <span id="article-102" class="article">Article 3</span>
  7. </body>
  8. </html>';
  9. $crawler = new Crawler();
  10. $crawler->addHtmlContent($html);
  11. $crawler->filterXPath('//span[contains(@id, "article-")]')->evaluate('substring-after(@id, "-")');
  12. /* Result:
  13. [
  14. 0 => '100',
  15. 1 => '101',
  16. 2 => '102',
  17. ];
  18. */
  19. $crawler->evaluate('substring-after(//span[contains(@id, "article-")]/@id, "-")');
  20. /* Result:
  21. [
  22. 0 => '100',
  23. ]
  24. */
  25. $crawler->filterXPath('//span[@class="article"]')->evaluate('count(@id)');
  26. /* Result:
  27. [
  28. 0 => 1.0,
  29. 1 => 1.0,
  30. 2 => 1.0,
  31. ]
  32. */
  33. $crawler->evaluate('count(//span[@class="article"])');
  34. /* Result:
  35. [
  36. 0 => 3.0,
  37. ]
  38. */
  39. $crawler->evaluate('//span[1]');
  40. // A Symfony\Component\DomCrawler\Crawler instance

Use the filter() method to find links by theiridorclassattributes and use theselectLink() method to find links by their content (it also finds clickable images with that content in its alt attribute).

Both methods return a Crawler instance with just the selected link. Use the link() method to get theSymfony\Component\DomCrawler\Link` object that represents the link:

  1. // first, select the link by id, class or content...
  2. $linkCrawler = $crawler->filter('#sign-up');
  3. $linkCrawler = $crawler->filter('.user-profile');
  4. $linkCrawler = $crawler->selectLink('Log in');
  5. // ...then, get the Link object:
  6. $link = $linkCrawler->link();
  7. // or do all this at once:
  8. $link = $crawler->filter('#sign-up')->link();
  9. $link = $crawler->filter('.user-profile')->link();
  10. $link = $crawler->selectLink('Log in')->link();

The Symfony\Component\DomCrawler\Link object has several useful methods to get more information about the selected link itself:

  1. // returns the proper URI that can be used to make another request
  2. $uri = $link->getUri();

Note

The getUri() is especially useful as it cleans thehrefvalue and transforms it into how it should really be processed. For example, for a link withhref=”#foo”, this would return the full URI of the current page suffixed with#foo. The return fromgetUri() is always a full URI that you can act on.

Images

To find an image by its alt attribute, use the selectImage method on an existing crawler. This returns a Crawler instance with just the selected image(s). Calling image() gives you a specialSymfony\Component\DomCrawler\Image` object:

  1. $imagesCrawler = $crawler->selectImage('Kitten');
  2. $image = $imagesCrawler->image();
  3. // or do this all at once
  4. $image = $crawler->selectImage('Kitten')->image();

The Symfony\Component\DomCrawler\Image object has the same getUri() method asSymfony\Component\DomCrawler\Link`.

Forms

Special treatment is also given to forms. A selectButton() method is available on the Crawler which returns another Crawler that matches