Tworzę scraper danych z użyciem phpDom i wszystko niby fajnie działa, wyciąga większość danych z html ale czasami wywala błąd:
Fatal error: Call to a member function getElementsByTagName() on a non-object
Błąd niby znajduje się w linii 263:
$rows = $description->item(0)->getElementsByTagName('tr');
Cała funkcja:
public function getProductImages($url) { $this->loginToWeb(); $domDocument = new DOMDocument(); libxml_use_internal_errors(true); curl_setopt($this->curlHandler, CURLOPT_POST, 0); curl_setopt($this->curlHandler, CURLOPT_URL, $url); $content = curl_exec($this->curlHandler); $domDocument->loadHTML($content); libxml_clear_errors(); $domXPath = new DOMXpath($domDocument); $thumbnails = $domXPath->query('//ul[@class="thumbnails"]'); if ($thumbnails->length > 0) { for ($c = 0; $c < $thumbnails->item(0)->childNodes->length; $c++) { if (get_class($thumbnails->item(0)->childNodes->item($c)) == 'DOMElement') { if ($thumbnails->item(0)->childNodes->item($c)->childNodes->length > 0) { $thumbsArray[] = str_replace('small_thumb', 'krampd_rd', $thumbnails->item(0)->childNodes->item($c)->childNodes->item(1)->childNodes->item(1)->getAttribute('src')); } } } $description = $domXPath->evaluate("/html/body//table[@id='attrsTable']"); $rows = $description->item(0)->getElementsByTagName('tr'); $html = '<table class="product_description">'; foreach ($rows as $row) { $html .= '<tr>'; $cells = $row->getElementsByTagName('td'); foreach ($cells as $cell) { $html .= '<td>'; $html .= $cell->nodeValue; $html .= '</td>'; } $html .= '</tr>'; } $html .= '</table>'; } }
Dziwne jest to że błąd wyskakuje tylko czasami i nie mam pojęcia dlaczego.
Ma ktoś jakieś pomysły ?