Skip to content

Extractors

Luc ALBERT edited this page Aug 22, 2016 · 2 revisions

Extractors is used to extract data to scraped content.

Extractors class must implements ExtractorInterface interface.

ChainExtractor

ChainExtractor allows you to chain multiple extractors to be used one after the other on the content provided. It is probably out of the extractor to be used most.

use Daric\Extractor\ChainExtractor;

$extractor = new ChainExtractor([
  new firstExtractor(),
  new secondExtractor(),
  ...
]);

$extractor->addExtractor(new otherExtractor());

Crawler* extractors

Work only with the content was an instance Symfony\Component\DomCrawler\Crawler (so during the first extractions).

CrawlerSelectorExtractor

Extract the content using a css selector.

Return Symfony\Component\DomCrawler\Crawler

use Daric\Extractor\CrawlerSelectorExtractor;

$extractor = new CrawlerSelectorExtractor('.cssSelector');

CrawlerNodeAttributeExtractor

Extract attribute of current content.

return string|array|null

use Daric\Extractor\CrawlerNodeAttributeExtractor;

$extractor = new CrawlerNodeAttributeExtractor('href'); // return href attribute of first node of the current selection
$extractor = new CrawlerNodeAttributeExtractor('href', 'array'); // return an array of all node of the current selection
$extractor = new CrawlerNodeAttributeExtractor('href', 'first'); // same without 'first' parameter
$extractor = new CrawlerNodeAttributeExtractor('href', 'last'); // return href attribute of last node of the current selection
$extractor = new CrawlerNodeAttributeExtractor('href', 'index', 1); // return href attribute of second node of the current selection

CrawlerNodeTextExtractor

Extract text of current node

return string|array|null

use Daric\Extractor\CrawlerNodeTextExtractor;

$extractor = new CrawlerNodeTextExtractor(); // return text value of first node of the current selection
$extractor = new CrawlerNodeTextExtractor('array'); // return an array of all node of the current selection
$extractor = new CrawlerNodeTextExtractor('first'); // same without 'first' parameter
$extractor = new CrawlerNodeTextExtractor('last'); // return text value of last node of the current selection
$extractor = new CrawlerNodeTextExtractor('index', 1); // return text value of second node of the current selection

CrawlerNodeHtmlExtractor

Extract outer html of current node

return string|array|null

use Daric\Extractor\CrawlerNodeHtmlExtractor;

$extractor = new CrawlerNodeHtmlExtractor(); // return html of first node of the current selection
$extractor = new CrawlerNodeHtmlExtractor('array'); // return an array of all node of the current selection
$extractor = new CrawlerNodeHtmlExtractor('first'); // same without 'first' parameter
$extractor = new CrawlerNodeHtmlExtractor('last'); // return html of last node of the current selection
$extractor = new CrawlerNodeHtmlExtractor('index', 1); // return html of second node of the current selection

CrawlerPreviousExtractor and CrawlerNextExtractor

Navigate in current content and return the previous siblings nodes or the next siblings node of the current selection.

Return Symfony\Component\DomCrawler\Crawler

use Daric\Extractor\CrawlerPreviousExtractor;

$extractor = new CrawlerPreviousExtractor();

CrawlerExtractorFactory

CrawlerExtractorFactory is a shorhand to create complex chain extrator for crawler.

use Daric\Extractor\CrawlerExtractorFactory;

// Simple selector

$extractor = CrawlerExtractorFactory::create('.selector');

// equivalent to
$extractor = new CrawlerSelectorExtractor('.selector');

// Extract attribute

$extractor = CrawlerExtractorFactory::create('.selector@attr');

// equivalent to
$extractor = new ChainExtractor([
  new CrawlerSelectorExtractor('.selector'),
  new CrawlerNodeAttributeExtractor('attr')
])

// Extract attribute with arguments

$extractor = CrawlerExtractorFactory::create('.selector@attr("index", 5)');

// equivalent to
$extractor = new ChainExtractor([
  new CrawlerSelectorExtractor('.selector'),
  new CrawlerNodeAttributeExtractor('attr', 'index', 5)
])

// Extract text

$extractor = CrawlerExtractorFactory::create('.selector@_text("array")');

// equivalent to
$extractor = new ChainExtractor([
  new CrawlerSelectorExtractor('.selector'),
  new CrawlerNodeTextExtractor('array')
])

// Extract html

$extractor = CrawlerExtractorFactory::create('.selector@_html("array")');

// equivalent to
$extractor = new ChainExtractor([
  new CrawlerSelectorExtractor('.selector'),
  new CrawlerNodeHtmlExtractor('array')
])

RegexExtractor

Use regular expression to extract content

return string|array|null

use Daric\Extractor\RegexExtractor;

$extractor = new RegexExtractor('/my (.*) pattern/i');

CustomExtractor

Use a closure to extract content.

CustomExtractor implements ScraperInjectorInterface : Scraper is available

use Daric\Extractor\CustomExtractor;

$extractor = new CustomExtractor(function($content, Scraper $scraper){
  return ...;
});

Create your own Extractor

Extractor must implements ExtractorInterface interface.

use Daric\Extractor\ExtractorInterface;

class MyExtractor implements ExtractorInterface
{
  public function extract($content)
  {
    // perform extraction
  }
}

If your extractor need the current scraper, just implements ScraperInjectorInterface

use Daric\Scraper;
use Daric\Extractor\ExtractorInterface;
use Daric\ScraperInjectorInterface;

class MyExtractor implements ExtractorInterface, ScraperInjectorInterface
{
  private $scraper;

  public function setScraper(Scraper $scraper)
  {
    $this->scraper = $scraper;
  } 

  public function extract($content)
  {
    // perform extraction
  }
}