douke6881 2015-05-26 02:06
浏览 47
已采纳

如何通过Simple Html Dom解析html部分中的多个元素

I am attempting to get various elements inside of an li as shown below. I am pretty new to this so I may not be using the most efficient methods but this is where I have started...

EXAMPLE CODE SIMPLIFIED....

<li id='entry_0' title='09879879'>
    <div ....>
        <h2> The title text would go here </h2>
        <span class='entrySize' ....> 20oz </span>
        <span class='entryPrice' ....> $32.09 </span>
        <span class='anotherEntry' ....> More Data I need To Grab </span>
        .......
    </div>
</li>

<li> .... With same structure as above .... 100's of entries like this </li>

I know how to pull individual parts separately but having trouble grasping how to do it grouped within a portion of the html.

$filename = "directory/file.html";
$html = file_get_html($filename);

for($i=0; $i<=count(entryNumber);$i++)
{
    $li_id = "entry_".$i;
    foreach($html->find('li[id='.$li_id.']') as $li) {         
        echo $li->innertext;
    }
}

So this gets me the content in the line item tag with the id number as the unique attribute. I would like to grab the h2 text, entrySize, entryPrice etc as I iterate through the line item tags. What I don't understand is once I have the line item tag content how can I parse through that line item inner tags and attributes. There maybe other parts of the full HTML document that has tags with same id, class as these throughout the document so I am breaking this down to portions and than looking to parse each section at a time.

I would also like to pull the title attribute out of the title tag for the li tag.

I hope my explanation make sense.

  • 写回答

1条回答 默认 最新

  • dongpankao6133 2015-05-26 03:18
    关注

    You should probably use a DOM parser. PHP comes bundled with one, and there are many other's you could use.

    http://php.net/dom

    PHP Simple HTML DOM Parser

    <?php
    $html = file_get_content($page);
    $doc = new DOMDocument();
    $doc->loadHTML($html);
    
    // now find what you need
    $items = $dom->getElementsByTagName('li');
    foreach ($items as $item) {
        $id = $item->getAttribute('id');
        if (strpos($id, 'item_') !== false) {
            // found matchin li, grab its children
        }
    }
    

    Use this as a baseline, we can't write all the code for you. Check out the PHP docs to finish this :) From what I have so far, you need to follow the docs to make it grab the child values, and handle them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决