Good day.

There is a function for parsing the site. The html_simple_dom_parser library is html_simple_dom_parser . I can parse the information I need about a single object, but I don’t understand how to loop a function so that I can transmit, for example, an array of references, and I was given a table with many objects.

Function code:

 <?php header('Content-Type: text/html; charset=UTF-8'); mb_internal_encoding('UTF-8'); mb_http_output('UTF-8'); mb_http_input('UTF-8'); mb_regex_encoding('UTF-8'); ini_set('display_errors', 'Off'); # подключаем библиотеку include('../simple_html_dom.php'); # глобальный массив, который будет заполняться информацией статьи $articles = array(); $URL = '____________________'; function getArticles($page) { global $urls, $articles, $descriptions; $html = new simple_html_dom(); $html->load_file($page); $items = $html->find('div[class=column]'); foreach($items as $post) { # выбираем и записываем в массив данные для парсинга $articles[] = array(price => $post->children(0)->children(1)->children(2)->outertext); $articles[] = array(price => $post->children(3)->children(1)->children(1)->outertext); $articles[] = array(price => $post->children(3)->children(1)->children(2)->outertext); $articles[] = array(price => $post->children(3)->children(1)->children(3)->outertext); $articles[] = array(price => $post->children(3)->children(1)->children(4)->outertext); $articles[] = array(price => $post->children(3)->children(1)->children(5)->outertext); $articles[] = array(price => $post->children(3)->children(1)->children(6)->outertext); $articles[] = array(price => $post->children(3)->children(1)->children(7)->outertext); $articles[] = array(price => $post->children(3)->children(1)->children(8)->outertext); $articles[] = array(price => $post->children(3)->children(1)->children(9)->outertext); $articles[] = array(price => $post->children(3)->children(2)->outertext); $articles[] = array(price => $post->children(3)->children(0)->find('a', 1)->outertext); } } ?> 

Call the function and draw the table:

 <?php getArticles($URL); $table = "<table border=1 class='table'><tbody><tr>"; #See here the start of the first row foreach($articles as $a => $item) { $table .= "<td>$item[price]</td>"; #double quotes for the variables if(($a+1) % 12 == 0) $table .= "</tr><tr>"; } $table .= "</tr></tbody></table>"; #end the row #append the text and don't overwrite it at the end echo $table; ?> 

Parsing result

Parsing result

Thanks in advance for your help.

    2 answers 2

    Rewrite function so

     function getArticles($page) { $articles = array(); $html = new simple_html_dom(); $html->load_file($page); $items = $html->find('div[class=column]'); foreach($items as $post) { $articles[] = array('price' => $post->children(0)->children(1)->children(2)->outertext); ............. $articles[] = array('price' => $post->children(3)->children(0)->find('a', 1)->outertext); } return $articles; } 

    Use so

     $urls = array(.........); $table = "<table border=1 class='table'><tbody><tr>"; foreach($urls as $url) { $articles = getArticles($url); foreach($articles as $a => $item) { $table. = "<td>$item[price]</td>"; if (($a + 1) % 12 == 0) $table. = "</tr><tr>"; } $table. = "</tr></tbody></table>"; } 
    • Thank you very much for the help. Few corrected the output of the table and closed the single quotes 'price' so as not to swear. Thank you @ anton-shchyrov! - optical-genius
    • @ optical-genius If my answer solved your problem, then mark it as correct - Anton Shchyrov

    I repeatedly encounter the same problem. When parsing the pages in the loop, the error "Call to a member function find () on null in D: \ OpenServer \ domains \ widgets \ parser \ prod \ server \ simple_html_dom.php on line 1129" occurs

     $html = new simple_html_dom(); $html->load_file($url); $html->save(); $response = array(); $subPageArr = array(); foreach ($html->find('tr') as $key => $value) { if ($key != 0) { $subPageArr[] = $value->find('.auction-list-address a', 0)->href; } } unset($html); function getArticles($page) { $articles = array(); $html = new simple_html_dom(); $html->load_file($page); $item = $html->find('.object-data'); return $item; } foreach($subPageArr as $url) { $articles = getArticles($url); echo $articles; } 

    what am I doing wrong? The essence of the task is as follows - The site has a bunch of links to other pages from which you need to parse data, I get an array of links without problems, but the parsing of these pages in a cycle breaks.