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);  
    
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改