【问题标题】:Parsing elements outside of divs to apply to arrays within Divs (Simple HTML Dom Parser)解析 div 外的元素以应用于 div 内的数组(简单的 HTML Dom 解析器)
【发布时间】:2014-08-04 00:18:14
【问题描述】:

我正在尝试学习 Simple HTML Dom Parser,作为介绍,我正在尝试解析 ESPN's college football score webpage 并将其转换为纯 HTML 表格。到目前为止,我已经能够做所有事情,除了导入日期。我遇到的问题是数据的结构如下:

<div class=gameDay-Container>
    <h4 class="games-date">Thursday, August 28 2014</h4>
    <div class=mod-ncf-scorebox> Containing games that I have scraped, which I want to add the date (Aug 28) to  </div>
    <h4 class="games-date">Friday, August 29 2014</h4>
    <div class=mod-ncf-scorebox> containing the next games that I will scrape, which I want the date (Aug 29) appended to </div>
</div>

顶部会抓取每个日期内的游戏数量并将其放入数组中,然后底部会编译表格的数据。 (这很混乱,因为我是通过反复试验来编写的;有没有更简单的方法来做到这一点?)

foreach($html_base->find('.gameDay-Container') as $dates) {
    $rows = $dates->find('.mod-ncf-scorebox');
    $count = count($rows);
    $supercount=$count.',';

    $megacount=explode(', ',$supercount);

    print_r($megacount);
}

foreach($html_base->find('.mod-ncf-scorebox') as $event) {
    $item['Date'] = ''; 
    $item['Away Team'] = $event->find('.team-name', 0)->plaintext;
    $item['Away Team'] = substr($item['Away Team'], 0, -1);
    $item['Away Score'] = $event->find('li.finalScore', 1);
    $item['Home Team'] = $event->find('.team-name', 1)->plaintext;
    $item['Home Team'] = substr($item['Home Team'], 0, -1);
    $item['Home Score'] = $event->find('li.finalScore', 2)->plaintext;
    $item['Game Status'] = $event->find('div.game-status', 0)->plaintext;
    $item['Game ID'] = $event->find('p', 0)->id;
    $item['Game ID'] = substr($item['Game ID'], 0, strpos( $item['Game ID'], '-'));
    $item['Week'] = $week;
    $item['League'] = 'NCAA Football';

    $NCAAFscores[] = $item;
}

问题:我可以将游戏日期添加到表格中的每一行吗?如果我必须使用数组设置,我可以从中获取值并进行某种计数吗?有没有更简单的方法让我完全忽略?

答案

根据下面 Enissay 的建议,我嵌套了 foreach 语句,效果非常好。这是最终的 sn-p 代码,以防万一有人有类似的事情。

foreach($html_base->find('.gameDay-Container') as $event2) {

    $date1 = $event2->prev_sibling()->plaintext; 
    $date2 = new DateTime($date1);
    $ymd = ($date2->format('Y-m-d'));


    foreach($event2->find('.mod-ncf-scorebox') as $event) {
        $item['Date']     = $ymd;
        $item['Away Team']     = $event->find('.team-name', 0)->plaintext;
        $item['Away Team'] = substr($item['Away Team'], 0, -1);
        $item['Away Score']     = $event->find('li.finalScore', 1);
        $item['Home Team']     = $event->find('.team-name', 1)->plaintext;
        $item['Home Team'] = substr($item['Home Team'], 0, -1);
        $item['Home Score']     = $event->find('li.finalScore', 2)->plaintext;
        $item['Game Status']    = $event->find('div.game-status', 0)->plaintext;
        $item['Game ID']    = $event->find('p', 0)->id;
        $item['Game ID'] = substr($item['Game ID'], 0, strpos( $item['Game ID'], '-'));
        $item['Week'] = $week;
        $item['League'] = 'NCAA Football';

        $NCAAFscores[] = $item;

}

}

【问题讨论】:

  • 我会说你可以使用strtotime(),比如strtotime($event-&gt;find('.games-date', 0)-&gt;plaintext)

标签: php mysql arrays simple-html-dom


【解决方案1】:

只需改变你的前进方式...

按照这个:

  1. 游戏被分组在div 块/容器中,每个前面都有相应的日期,获取这些块▶$html_base-&gt;find('.gameDay-Container')

  2. 对于每个区块,您可以获得日期▶$block-&gt;prev_sibling()-&gt;plaintext;

  3. 然后你会发现该区块内的所有游戏

    foreach( $block-&gt;find('.mod-ncf-scorebox') as $game ) { ... }

未经测试,但应该可以工作:)

【讨论】:

    猜你喜欢
    • 2015-01-07
    • 1970-01-01
    • 2015-02-19
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 2012-08-28
    相关资源
    最近更新 更多