Search code examples
phphtmllistparsingsimple-html-dom

parse a html heterogeneous list in php using simple_html_dom


I want to parse a html list consist of article tag and I found simple_html_dom I read the doc but can't make it going well
I have pages like

 <article class="post">      
        <div class="leftc">  <figure class="ico-2721"></figure>
            <header>
                <h2>
                    <a href="http://pop-music.ir/%d8%a2%d9%87%d9%86%da%af-%d8%ac%d8%af%db%8c%d8%af-%d9%85%db%8c%d9%84%d8%a7%d8%af-%d8%a8%d8%a7%d8%a8%d8%a7%db%8c%db%8c-%d8%a8%d9%86%d8%a7%d9%85-%db%8c%d8%a7%d8%aa%d9%88-%db%8c%d8%a7%d8%aa%d9%88"
                        rel="bookmark" title="دانلود آهنگ جدید میلاد بابایی بنام یاتو یاتو">دانلود آهنگ جدید میلاد بابایی بنام یاتو یاتو</a>
                </h2>
                <span class="cat">دسته بندی :
                    <a href="http://pop-music.ir/category/single-music/happy-song" rel="category tag">آهنگ شاد</a>,
                    <a href="http://pop-music.ir/category/single-music" rel="category tag">تک آهنگ</a>
                </span>
                <span class="date">تاریخ : ۲۱ام اسفند, ۱۳۹۶ </span>
            </header>
            <p style="text-align: center;">
                <img class="alignnone" title="دانلود آهنگ جدید میلاد بابایی بنام یاتو یاتو" src="http://dl.pop-music.ir/images/1396/Esfand/Milad-Babaei.jpg"
                    alt="دانلود آهنگ جدید میلاد بابایی بنام یاتو یاتو" width="500" height="500">
            </p>
            <p style="text-align: center;">
                <a title="دانلود آهنگ جدید" href="http://pop-music.ir/" target="_blank" rel="noopener noreferrer">دانلود آهنگ جدید</a>
                <strong>میلاد بابایی</strong> بنام
                <strong>یاتو یاتو</strong> با بالاترین کیفیت</p>
            <p style="text-align: center;">
                <a href="http://pop-music.ir/" target="_blank" rel="noopener noreferrer">Download New Music</a>
                <a href="http://pop-music.ir/tag/download-new-music-milad-babaei">Milad Babaei</a> – Yato Yato</p>
            <p style="text-align: center;">
                <span style="color: #0000ff;">ترانه: عاطفه حبیبی , موزیک: میلاد بابایی , تنظیم: امیر ارشیا</span>
            </p>
            <div style="text-align: center;">
                <div>
                    <p>برای
                        <a title="دانلود آهنگ" href="http://pop-music.ir/tag/%D8%AF%D8%A7%D9%86%D9%84%D9%88%D8%AF-%D8%A2%D9%87%D9%86%DA%AF"
                            target="_blank" rel="noopener noreferrer">دانلود آهنگ</a> به ادامه مطلب مراجعه کنید …</p>
                </div>
            </div>
            <p style="text-align: center;">
            </p>
            <div class="morelink">
                <a href="http://pop-music.ir/%d8%a2%d9%87%d9%86%da%af-%d8%ac%d8%af%db%8c%d8%af-%d9%85%db%8c%d9%84%d8%a7%d8%af-%d8%a8%d8%a7%d8%a8%d8%a7%db%8c%db%8c-%d8%a8%d9%86%d8%a7%d9%85-%db%8c%d8%a7%d8%aa%d9%88-%db%8c%d8%a7%d8%aa%d9%88">دانلود + ادامه مطلب</a>
                <div class="rating"></div>
            </div>
            <div class="pull-left"></div>
            <div class="clear"></div>
        </div>

        <div class="clear"></div>

    </article>

i write this kind of code even i know it's not good but don't have other option

$html = new simple_html_dom();
$link=array();
foreach ($html->find('article img') as $i => $element) {
    $links[$i]['src'] = $element->src;
}
$element = $html->find('article span[class=cat] a');
for ($i = 0; $i <= sizeof($element) / 2 - 1; $i++) {
    $links[$i]['cat'][2 * $i + 1] = $element[2 * $i + 1]->href;
    $links[$i]['cat'][2 * $i] = $element[2 * $i]->href;
}

cat items are not fixed by doing some have one some have two or more and some have any, there many fields like this
I want to do something like this for performance and easier parsing but don't know how

$arti=$html->find('article') 
  foreach ($arti as $i => $element) {
        $links[$i]['src'] = $element->img->src;
        foreach ($arti->find('cat') as $j => $element) {
           $links[$i]['cat'][$j] = $element->href;
          }
    }

Solution

  • there is a workaround but not perfect answer,my solution is to get outtertext or html of each article and create other instance using simple_htm_dom

    $html2 = str_get_html($html->find('article')->outertext);
    

    and then send html2 to other function to parse it .
    it not perfect answer so waiting for better answers