diff --git a/src/Page/GoogleSerp.php b/src/Page/GoogleSerp.php index 310a2d73..60f7d6b8 100644 --- a/src/Page/GoogleSerp.php +++ b/src/Page/GoogleSerp.php @@ -13,6 +13,7 @@ use Serps\SearchEngine\Google\Parser\Evaluated\NaturalParser as EvaluatedNaturalParser; use Serps\SearchEngine\Google\Parser\Raw\NaturalParser as RawNaturalParser; use Serps\Stubs\RelatedSearch; +use Serps\SearchEngine\Google\Parser\Evaluated\WikiBoxParser; class GoogleSerp extends GoogleDom { @@ -134,4 +135,17 @@ public function getRelatedSearches() return $relatedSearches; } + + /** + * @return \Serps\Core\Serp\IndexedResultSet + */ + public function getWikiBox() + { + $result = null; + if ($this->javascriptIsEvaluated()) { + $parser = new WikiBoxParser(); + $result = $parser->parse($this); + } + return $result; + } } diff --git a/src/Parser/Evaluated/Rule/WikiBox/Description.php b/src/Parser/Evaluated/Rule/WikiBox/Description.php new file mode 100644 index 00000000..8f5afa08 --- /dev/null +++ b/src/Parser/Evaluated/Rule/WikiBox/Description.php @@ -0,0 +1,56 @@ +getAttribute('class') == '_G1d _wle _xle' ? self::RULE_MATCH_MATCHED : self::RULE_MATCH_NOMATCH; + } + + /** + * @param GoogleDom $dom + * @param \DOMElement $node + * @param IndexedResultSet $resultSet + */ + public function parse(GoogleDom $dom, \DOMElement $node, IndexedResultSet $resultSet) + { + $xpath = $dom->getXpath(); + + /* @var $description \DOMElement */ + $description = $xpath + ->query(Css::toXPath('.kno-rdesc span:first-child'), $node) + ->item(0); + + $wikiLink = $xpath + ->query(Css::toXPath('.kno-rdesc span:last-child a'), $node) + ->item(0); + + $data = [ + 'description' => $description ? $description->nodeValue : null, + 'wikipedia_link' => $wikiLink ? $wikiLink->getAttribute('href') : null, + ]; + + $item = new BaseResult('description', $data); + $resultSet->addItem($item); + } +} diff --git a/src/Parser/Evaluated/Rule/WikiBox/Header.php b/src/Parser/Evaluated/Rule/WikiBox/Header.php new file mode 100644 index 00000000..7840d02d --- /dev/null +++ b/src/Parser/Evaluated/Rule/WikiBox/Header.php @@ -0,0 +1,77 @@ +getAttribute('class') == 'kp-header' ? self::RULE_MATCH_MATCHED : self::RULE_MATCH_NOMATCH; + } + + /** + * @param GoogleDom $dom + * @param \DOMElement $node + * @param IndexedResultSet $resultSet + */ + public function parse(GoogleDom $dom, \DOMElement $node, IndexedResultSet $resultSet) + { + $xpath = $dom->getXpath(); + + /* @var $title \DOMElement */ + $title = $xpath + ->query(Css::toXPath('.kp-hc .kno-ecr-pt.kno-fb-ctx'), $node) + ->item(0); + + $description = $xpath + ->query(Css::toXPath('.kp-hc ._gdf._LAf span'), $node) + ->item(0); + + $map = $xpath + ->query(Css::toXPath('#media_result_group ._tN._eXg a'), $node) // .kno-fb-ctx .rhsg4.rhsmap5col a img + ->item(0); + + $mapData = null; + if (null !== $map) { + $url = $map->getAttribute('href'); + $placeName = null; + $placeData = null; + if (preg_match('/maps\/place\/(.*?)\/data=(.*?)\?/', $url, $matches)) { + $placeName = $matches[1]; + $placeData = $matches[2]; + } + $mapData = [ + 'url' => $url, + 'placeName' => $placeName, + 'placeData' => $placeData, + ]; + } + + $data = [ + 'title' => $title ? $title->nodeValue : null, + 'description' => $description ? $description->nodeValue : null, + 'map' => $mapData, + ]; + + $item = new BaseResult('header', $data); + $resultSet->addItem($item); + } +} diff --git a/src/Parser/Evaluated/WikiBoxParser.php b/src/Parser/Evaluated/WikiBoxParser.php new file mode 100644 index 00000000..89a27efc --- /dev/null +++ b/src/Parser/Evaluated/WikiBoxParser.php @@ -0,0 +1,40 @@ +getXpath(); + $xpath = Css::toXPath('div#rhs div._OKe > div > div'); + + return $xpathObject->query($xpath); + } +}