/** * Fetch this comic */ public function fetch() { $html = @file_get_contents($this->url); if ($html) { $crawler = new \Symfony\Component\DomCrawler\Crawler(); $crawler->addContent($html); $f = $crawler->filter($this->filter); if ($f->count()) { $this->imageUrl = $this->prefix . $f->attr($this->attr); } } }
/** * Gets a Crawler object. * * @return \Symfony\Component\DomCrawler\Crawler */ public function getCrawler() { if (!class_exists('Symfony\\Component\\DomCrawler\\Crawler')) { trigger_error('The Symfony\\Component\\DomCrawler\\Crawler object is not installed'); return null; } $crawler = new \Symfony\Component\DomCrawler\Crawler(null, $this->getUrl()); $crawler->addContent($this->getRawContents(), $this->getContentType()); return $crawler; }
#!/usr/bin/php <?php $config = ['require_services' => ['sf_css_selector'], 'git_urls' => ['https://github.com/yfix/DomCrawler.git' => 'sf_dom_crawler/'], 'autoload_config' => ['sf_dom_crawler/' => 'Symfony\\Component\\DomCrawler'], 'example' => function () { $crawler = new \Symfony\Component\DomCrawler\Crawler(); $crawler->addContent('<html><body><p>Hello World!</p></body></html>'); echo $crawler->filterXPath('descendant-or-self::body/p')->text(); echo PHP_EOL; echo $crawler->filter('body > p')->text(); // require css selector echo PHP_EOL; }]; if ($return_config) { return $config; } require_once __DIR__ . '/_yf_autoloader.php'; new yf_autoloader($config);
protected function standardizeProps(&$result) { $autoColor = (int) $this->getOption('auto_card_color', 1); if ($autoColor && isset($result['favicon_colors'])) { $color = $this->getOption('default_card_color', '#D71212'); foreach ($result['favicon_colors'] as $colors) { $min = min($colors['color']); $max = max($colors['color']); $color = 'rgb(' . implode(',', $colors['color']) . ')'; if ($max - $min > 10) { break; } } $result['color'] = $color; } if (isset($result['authors']) && !empty($result['authors'])) { $result['author_name'] = $result['authors'][0]['name']; $result['author_url'] = $result['authors'][0]['url']; } if (isset($result['media']['type'])) { switch ($result['media']['type']) { case 'photo': $result['type'] = 'photo'; break; case 'video': $result['type'] = 'video'; break; } } if (isset($result['images'][0]['url'])) { $result['thumbnail_url'] = $result['images'][0]['url']; $width = $this->getMaxWidth(); if (isset($result['images'][0]['width']) && $result['images'][0]['width'] < $width - 50) { $result['thumbnail_type'] = 'small'; } $result['thumbnail_width'] = $width; } if (strtolower($result['provider_name']) == 'amazon') { if (isset($result['media']['html'])) { $crawler = new \Symfony\Component\DomCrawler\Crawler(); $crawler->addContent($result['media']['html']); try { $img = $crawler->filter('tr > td')->first()->filter('img')->attr('src'); } catch (\Exception $e) { $img = ''; } try { $subHead = $crawler->filter('span.subhead')->text(); } catch (\Exception $e) { $subHead = ''; } try { $listPrice = $crawler->filter('td.listprice')->text(); } catch (\Exception $e) { $listPrice = ''; } try { $price = $crawler->filter('td.price')->text(); } catch (\Exception $e) { $price = ''; } try { $saved = $crawler->filter('td.saved')->text(); } catch (\Exception $e) { $saved = ''; } $result['amazon'] = array('img' => $img, 'subHead' => $subHead, 'listPrice' => $listPrice, 'price' => $price, 'saved' => $saved); } else { $img = ''; if (isset($result['images'][0]['url'])) { $img = $result['images'][0]['url']; } $result['amazon'] = array('img' => $img, 'subHead' => '', 'listPrice' => '', 'price' => '', 'saved' => ''); } if ($autoColor) { $result['color'] = 'rgb(254, 167, 7)'; } } }