【phpQuery】采集数据 以及乱码处理

1、下载【phpQuery】,网上自行查找

2、把下载好的文件找到phpQuery.php文件的目录,把改目录的所有文件复制到项目指定文件夹

示例:【CI】框架

/**
     *-------------------------------------
     * @Function                 soccerDataEnd
     * @Author                   acclea
     * @Date                     2019/5/15
     * @Time                     10:56:11
     * @Describe                 已经结束的比赛
     * @Param      int|string
     * @Return     array|string
     *-------------------------------------
     */
    public function soccerDataEnd(){
        $dates  = $this->input->get("dates");
        if(!$dates){ $dates = date("Y-m-d");}
//        if($dates != date("Y-m-d",strtotime($dates))){ error("日期有误"); }
        header( 'Content-Type:text/html;charset=utf-8');
        $url    = "https://info.sporttery.cn/football/match_result.php?";
        $param  = "start_date=2019-05-01&end_date=2019-05-01&search_league=0";
//        $param  = "start_date=$dates&end_date=$dates&search_league=0";

        require dirname(dirname(__FILE__)).("/libraries/phpQuery.php");
        phpQuery::newDocumentFile($url.$param);

        $table = pq(".m-tab tr");

        $objArr     = array();

        foreach($table as $trKey => $tr){
            if($trKey == count($table) -2){break;}
            $matchTime      =  pq($tr)->find("td:eq(0)")->html();
            $matchName      =  pq($tr)->find("td:eq(2)")->attr("title");
            $masterTeam     =  pq($tr)->find("td:eq(3) span:eq(0)")->attr("title");
            $rangqiu        =  pq($tr)->find("td:eq(3) span:eq(0)")->html();
            $rangqiu        = iconv('GBK', 'UTF-8', $rangqiu);
            $guestTeam      =  pq($tr)->find("td:eq(3) span:eq(2)")->attr("title");

            $resultHalf     =  pq($tr)->find("td:eq(4) span:eq(0)")->html();
            $resultAll      =  pq($tr)->find("td:eq(5) span:eq(0)")->html();
            $hostWin        =  pq($tr)->find("td:eq(6) span:eq(0)")->html();
            $dogfall        =  pq($tr)->find("td:eq(7) span:eq(0)")->html();
            $guestWin       =  pq($tr)->find("td:eq(8) span:eq(0)")->html();

            $objVal         = array(
                'matchTime'     => $matchTime,
                'matchName'     => $matchName,
                'masterTeam'    => $masterTeam,
                'rangqiu'       => $rangqiu,
                'guestTeam'     => $guestTeam,

                'resultHalf'    => $resultHalf,
                'resultAll'     => $resultAll,
                'hostWin'       => $hostWin,
                'dogfall'       => $dogfall,
                'guestWin'      => $guestWin,

                'addtime'       => time(),
                'times'         => $dates ?  strtotime($dates) : time(),
            );

            $objArr[]   = $objVal;
        }
        $finishFile     = dirname(dirname(dirname(__FILE__))).("/grasp/finish-$dates.json");
        $finishMatch    = fopen($finishFile, "w") or die("Unable to open file!");
        fwrite($finishMatch, json_encode($objArr,JSON_PRETTY_PRINT));
        fclose($finishMatch); 

        //打印
        echo "<pre>?";
        print_r($objArr);

    }

 

3、乱码处理

$content = mb_convert_encoding($content,'ISO-8859-1','utf-8');
$content = mb_convert_encoding($content,'utf-8','GBK');

转载于:https://my.oschina.net/u/3268486/blog/3050252

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值