分析外部网站表


Parse external website table

上有一个日历:http://www.friendsbalt.org/upper/stulife/calendar.asp以静态表的形式,我希望有一个服务器抓取并逐行解析表。这可能吗?你将如何以最有效的方式做到这一点?代码示例将是惊人的。

如果希望通过网页完成,可以使用类似Simple HTML DOM for php的东西。

require "simple_html_dom.php"; //Get this file from the link above
$html = file_get_html("http://example.com");
$data = array();
foreach($html->find("table tr") as $tr){
    $row = array();
    foreach($tr->find("td") as $td){
        /* enter code here */
        $row[] = $td->plaintext;
    }
    $data[] = $row;
}

然后所有的数据都将在$data变量中。

var_dump($data); //To prove it works.

我会考虑将其放在"刷新"脚本中,并将所有信息保存到数据库中。然后你可以从数据库中获取信息,这几乎是即时的。

然后,如果您愿意,您可以制作一个cron脚本,使其每小时运行一次——更新数据库,使其中的信息保持新鲜。

这实际上取决于你想用它做什么:)