Parse external website table

2019-06-12 11:20发布

There is a calendar on: http://www.friendsbalt.org/upper/stulife/calendar.asp in the form of a static table, I want to have a server grab and parse out the table row by row. Is this possible? How would you do it the most efficient way possible? Code examples would be amazing.

1条回答
该账号已被封号
2楼-- · 2019-06-12 11:40

You could use something like Simple HTML DOM for php if you wanted it to be done by a web page.

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;
}

Then all of the data will be in the $data variable.

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

I would consider putting this in a 'refresh' script, and saving all the info to a database. Then you can just fetch the info from the database - which will be nearly instant.

Then, if you wanted, you could make a cron script to make this run ever hour by itself - updating the database so that the information in it stays fresh.

It really depends what you want to do with it :)

查看更多
登录 后发表回答