doxp30826 2018-01-21 23:51 采纳率: 0%
浏览 74
已采纳

具有多个表的PHP简单HTML DOM解析器

I am trying to parse html data from a local weather channels site to get closing informations for schools, businesses, and churches around my local area.

I have run into the problem though that the information is contained in tables that do not have an id that I can use to identify them. Below I have included an example of what one of their html tables looks like. Is it possible to parse multiple HTML tables like this and pull the containing data using HTML DOM Parser with PHP. I have read through this documentation, but can't seem to find an applicable solution.

Thanks!

EDIT: I should probably also specify I want to take this data and be able to parse it to JSON data to use to load in an application. So basically have the organizations name and then the status that I can fetch from a JSON page.

Link to weather channels site

<table class="table table-condensed table-striped">
  <tbody>
    <tr>
      <th class="span5">Organization</th>
      <th class="span9">Status</th>
    </tr>
    <tr>
      <td><b>BEACON HOPE CHURCH - GRAND ISLAND</b></td>
      <td>Activity Canceled Sunday<small>: No Evening Classes</small></td>
    </tr>
    <tr>
      <td><b>PRINCE OF PEACE CATHOLIC (KEARNEY)</b></td>
      <td>Closed Monday<small>: SUNDAY EVENING ACTIVITIES CANCELED, NO MON. MORNING MASS, OFFICES CLOSED MON.</small></td>
    </tr>
  </tbody>
</table>
  • 写回答

1条回答 默认 最新

  • dongleibeng5602 2018-01-22 00:46
    关注

    Found the answer to my question with help from user sms who commented above. This php pulls the data from the first table and encodes it in the JSON format.

    <?php
    
    include('simple_html_dom.php');
    header('Content-Type: application/json');
    
    $html = file_get_html('http://www.1011now.com/weather/closings');
    $row_count=0;
    $json = array();
    
    // Find all links 
    $table = $html->find('table', 0);
    foreach($table->find('tr') as $row) {
        $name = $row->find('td',0)->innertext;
        $status = $row->find('td',1)->innertext;
    
        $json[] = [ 'name' => strip_tags($name), 'status' => strip_tags($status)];
    }
    
    $options = array(
        'http' => array(
        'method'  => 'POST',
        'content' => json_encode(array('Closings' =>$json)),
        'header'=>  "Content-Type: application/json
    " .
                    "Accept: application/json
    "
        )
    );
    
    $context  = stream_context_create( $options );
    $result = file_get_contents( $url, false, $context );
    $response = json_decode( $result );
    
    echo json_encode(array('Closings' =>$json), JSON_PRETTY_PRINT);  
    
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗