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); }
/** * Replace child node * * @param $string * * @return $this */ protected function replaceChild($string) { if (!empty($string)) { $newDocument = new Document($string); if ($newDocument->outertext != $string) { throw new RuntimeException("Not valid HTML fragment"); } } foreach ($this->node->childNodes as $node) { $this->node->removeChild($node); } if (!empty($string)) { $newNode = $this->node->ownerDocument->importNode($newDocument->getDocument()->documentElement, true); $this->node->appendChild($newNode); } return $this; }
public function testClear() { $document = new Document(); $this->assertNull($document->clear()); }
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)); }
/** * @param $name * @param $arguments * * @return bool|Document */ public static function __callStatic($name, $arguments) { if ($name == 'str_get_html') { $document = new Document(); return $document->loadHtml($arguments[0]); } if ($name == 'file_get_html') { $document = new Document(); return $document->loadHtmlFile($arguments[0]); } throw new BadMethodCallException('Method does not exist'); }