Esempio n. 1
0
 private function _parseCategory($url, $category)
 {
     $products = [];
     try {
         $html = new Document(self::request($url . '?ajax=1&page=1'));
     } catch (\Exception $e) {
         return $products;
     }
     foreach ($html->find('div.products_card') as $item) {
         //$item_url = $item->find('a.fancy_ajax', 1)->url;
         $title = $item->find('a.fancy_ajax', 0)->plaintext;
         $article = $item->find('input[name=product_id]', 0)->value;
         $unitCountry = $item->find('div.small_country', 0);
         $product_country = $unitCountry ? $unitCountry->plaintext : null;
         $unitItem = $item->find('div.form_elements input', 0);
         $unit = $unitItem ? $unitItem->getAttribute('data-measure') : 'шт';
         $unitPrice = $item->find('div.price', 0);
         if ($unitPrice->find('div', 0)) {
             $unitPrice->find('div', 0)->innertext = '';
         }
         $price = (int) str_replace(' ', '', $unitPrice->plaintext);
         $image = $item->find('img', 0)->src;
         $res = preg_match('/([\\w\\W]+) ([\\d]+) (?=(г|кг|шт))|([\\w\\W]+)/ui', str_replace(' ', ' ', $title), $matches);
         if ($res) {
             $products[] = ['category_id' => $category, 'title' => $matches[1] ? $matches[1] : $matches[4], 'price' => $price, 'mass' => $matches[1] ? $matches[3] === 'кг' ? $matches[2] * 1000 : $matches[2] : null, 'article' => $article, 'country' => $product_country, 'image' => $image, 'unit' => $unit];
         } else {
             Yii::error("[Parser] e-dostavka.by {$url} {$article}");
         }
     }
     return $products;
 }
 public function testText()
 {
     $html = '<div><p>foo</p><p>bar</p></div>';
     $document = new Document($html);
     $element = $document->find('p');
     $this->assertEquals('foobar', $element->text());
     $this->assertEquals('foobar', $element->plaintext);
 }
 /**
  * @dataProvider findTests
  */
 public function testFind($html, $selector, $count)
 {
     $document = new Document($html);
     $elements = $document->find($selector);
     $this->assertInstanceOf('FastSimpleHTMLDom\\NodeList', $elements);
     $this->assertEquals($count, count($elements));
     foreach ($elements as $element) {
         $this->assertInstanceOf('FastSimpleHTMLDom\\Element', $element);
     }
     if ($count !== 0) {
         $element = $document->find($selector, -1);
         $this->assertInstanceOf('FastSimpleHTMLDom\\Element', $element);
     }
 }
 public function testHasAttribute()
 {
     $html = '<div class="post" id="p1">foo</div>';
     $document = new Document($html);
     $element = $document->find('div', 0);
     $this->assertTrue($element->hasAttribute('class'));
     $this->assertTrue(isset($element->id));
 }