1

I am trying to use Symfony Crawler.

So I have checked this article.

What I want to do is to get the 3,335.00(The second argument)

For now, I try sentence like this, but it is wrong.

$crawler = $crawler->filter('body > div[@class="cell_label"]');

How can I do it??

<body>
<div class="cell__label">  Value1  </div> <div class="cell__value cell__value_"> 2,355.00 </div>
<div class="cell__label">  Value2  </div> <div class="cell__value cell__value_"> 3,355.00 </div>
<div class="cell__label">  Value3  </div> <div class="cell__value cell__value_"> 4,355.00 </div>
</body>

$crawler = new Crawler($url);
$crawler = $crawler->filter('body > div[@class="cell_label"]');//// no work...

foreach ($crawler as $domElement) {
    var_dump($domElement);
}
genesst
  • 1,333
  • 1
  • 10
  • 39
whitebear
  • 11,200
  • 24
  • 114
  • 237

2 Answers2

1

I can see several issue here:

  1. Using $crawler->filter() implies you must pass a css selector as a parameter, not XPath expressions, so use 'body > div.cell__label' or 'body div[class^="cell__"]' if you need to select all div with a class that starts with cell__, btw you have a typo in cell_label (one underscore).

  2. The Crawler accepts DOMNodeList, DOMNode, array or string as a constructor parameters, not a url to a remote resource (but I assume it may be just an arbitrary variable name you used there). Technically url is a string as well, but not XML formatted string.

  3. If you want to use XPath expression use $crawler->filterXPath(), like that:

    $nodes = $crawler->filterXPath('//div[contains(@class, "cell__label")]');

Here's a documentation on how to use XPath - https://www.w3.org/TR/xpath/

genesst
  • 1,333
  • 1
  • 10
  • 39
  • THank you this quite helps. And as you mentioned. I wrongly used about `$url`. I use not `$url` but `$html` after `$html = @file_get_contents($url);` – whitebear Sep 21 '17 at 08:57
0

Crawler filter can handle jQuery like selectors, so you can:

$crawler = $crawler->filter('.cell__value');