具有多个表的PHP简单HTML DOM解析器
我正在尝试从本地气象频道站点解析html数据,以获取本地区附近学校,企业和教堂的关闭信息.
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.
我遇到了一个问题,尽管信息包含在没有ID的表中,我无法使用该ID来识别它们.下面,我提供了一个有关其html表外观的示例.是否可以解析多个HTML表,并使用带有PHP的HTML DOM Parser提取包含的数据.我已阅读此文档,但似乎找不到适用的解决方案.
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.
谢谢!
我可能还应该指定我要获取此数据,并能够将其解析为JSON数据以用于在应用程序中加载.因此,基本上有组织名称,然后是我可以从JSON页面获取的状态.
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.
<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>
在上面发表评论的用户短信的帮助下找到了我的问题的答案.该php从第一个表中提取数据,并以JSON格式对其进行编码.
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\r\n" .
"Accept: application/json\r\n"
)
);
$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);
?>