Skip to content

Commit

Permalink
Extract Local Services Block
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinDev committed Apr 24, 2023
1 parent cc5e21d commit b209b0a
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions packages/google/src/Extractor/SERPExtractor.php
Expand Up @@ -93,10 +93,8 @@ public function getAlsoAsked(): array
public function extractBusinessResults(): array
{
$selector = '[data-rc_ludocids]';

$nodes = $this->domCrawler->filter($selector);
$mapsResults = [];

$i = 0;
foreach ($nodes as $node) {
if (! $node instanceof \DOMElement) {
Expand All @@ -118,14 +116,47 @@ public function extractBusinessResults(): array
++$i;
}

if (\count($mapsResults) < 1) {
return $this->extractLocalServiceResults();
}

return $mapsResults;
}

/**
* @return BusinessResult[]
*/
private function extractLocalServiceResults(): array
{
$selector = '.rllt__details [role="heading"]';
$nodes = $this->domCrawler->filter($selector);
$mapsResults = [];
$i = 0;
foreach ($nodes as $node) {
if (! $node instanceof \DOMElement) {
continue;
}
if ('' === $node->textContent) {
continue;
}

$mapsResults[$i] = new BusinessResult();
$mapsResults[$i]->cid = '0';
$mapsResults[$i]->name = trim(Helper::htmlToPlainText($node->textContent));
$mapsResults[$i]->position = $i + 1;
$mapsResults[$i]->organicPos = $i + 1;
$mapsResults[$i]->pixelPos = $this->getPixelPosFor($node->getNodePath() ?? '');
++$i;
}

return $mapsResults;
}

private function extractBusinessName(\DOMElement $node): string
{
$nameNode = (new Crawler($node))->filter('span')->getNode(0);

return null !== $nameNode ? $nameNode->textContent : '';
return null !== $nameNode ? trim(Helper::htmlToPlainText($nameNode->textContent)) : '';
}

/**
Expand Down

0 comments on commit b209b0a

Please sign in to comment.