自动将表上传到数据库[关闭]

自动将表上传到数据库[关闭]

问题描述:

How can I automatically upload to a database data from tables like the one in this page? I can use their function "export" and then manually download the .csv file, and upload it, but if I want everyday the data from each game, it is a pain... do you think it is possible to automatize it? The only solution would be by scraping their website?

Thanks

如何从表格中自动上传到数据库数据页面? 我可以使用他们的功能“导出”,然后手动下载.csv文件,并上传它,但如果我每天都想要每个游戏的数据,这是一个痛苦......你认为它可以自动化吗? 唯一的解决方案是抓取他们的网站? p>

谢谢 p> div>

You could use the cURL lib of PHP.

Here there's an example:

<?php
    $ch = curl_init();
    $timeout = 0; // set to zero for no timeout
    $url = 'http://www.basketball-reference.com/boxscores/201506160CLE.html'; // set the page url
    curl_setopt ($ch, CURLOPT_URL, $url);//enter your url here
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents = curl_exec($ch); //get the page contents
    include "simple_html_dom.php";
    $table = $file_contents;
    $html = str_get_html($table);
    $printing = false;
    //header('Content-type: application/ms-excel');
    $fp = fopen("php://output", "w");
    foreach($html->find('tr') as $element){
        $arr = array();
        foreach ($element->find('tr') as $element2) {
                    if(!$printing)
                        $printing = strpos($element2,'Scoring') !== false;
                    if($printing){
                        //echo $element2 -> plaintext . "<br>";
                        $arr[] = $element2 -> plaintext; //comment here
                    }

        }
        fputcsv($fp, $arr);
        }

        fclose($fp);
    ?>

You have to download a file from here. You get a text/csv file in your page, if you prefere a plain it's sufficient switch comments (comment line where is requested and decomment the others)

You can parse your new csv as you want