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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值