IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> php 抓取 百度、微博、搜狗热门关键词 -> 正文阅读

[PHP知识库]php 抓取 百度、微博、搜狗热门关键词

/**
     * 获取 百度 关键词
     */
    public function index(){
        $data = $this->getBaiduHotKeyWord();
        // 查询 数据库
        $where['how_type'] = 4;
        $saveData = [];
        for ($i = 0;$i<10;$i++){
            $saveData[$i] = [
                'howid' => $i +1,
                'how_type' => 4,
                'how_words' => $data[$i],
                'sort' => $i+1,
                'url' => 'https://www.baidu.com/s?wd='.$data[$i],
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s'),
            ];
        }
        $model = M('udc_out_hotwords');
        if ($model->where($where)->select()){
            for ($j = 0;$j<10;$j++) {
                $where['howid'] = $j +1;
                unset($saveData[$j]['howid']);
                $model->where($where)->save($saveData[$j]); // 根据条件更新记录
            }
        }else{
            $model->addAll($saveData);
        }
        Log::write($model->getError());
    }

    /**
     * 获取 搜狗关键词
     */
    public function souGou(){
        $html = $this->getUrlContent("http://top.sogou.com/hot/shishi_1.html?fr=tph_righ");
        $newTable = $this->getSouGouHtml($html);
        //print_r($data);

        $data = $newTable;
        foreach ($data as $key=>&$datum){
            if ($key<3){
                $datum = mb_substr($datum[2],0,8);
            }else{
                $datum = $datum[1];
            }
        }
        // 查询 数据库
        $where['how_type'] = 1;
        $saveData = [];
        for ($i = 0;$i<10;$i++){
            $saveData[$i] = [
                'howid' => $i +1+20,
                'how_type' => 1,
                'how_words' => $data[$i],
                'sort' => $i+1,
                'url' => 'https://www.sogou.com/sogou?query='.$data[$i],
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s'),
            ];
        }
        $model = M('udc_out_hotwords');
        if ($model->where($where)->select()){
            for ($j = 0;$j<10;$j++) {
                $where['howid'] = $j +1+20;
                unset($saveData[$j]['howid']);
                $model->where($where)->save($saveData[$j]); // 根据条件更新记录
            }
        }else{
            $model->addAll($saveData);
        }
        Log::write($model->getError());
    }


    /**
     * 获取 微博 关键词
     */
    public function wei(){
        $html = $this->getUrlContent("https://s.weibo.com/top/summary?Refer=top_hot&topnav=1&wvr=6");
        $table = $this->getTable($html);
        $table = array_slice($table,2); # 把前面多余部分截掉
        $newTable = array_column($table,'1');
        foreach ($newTable as &$value){
            $str = trim($value);
            $str1 = preg_replace('/\s+/','+',$str);
            $arr = explode ('+',$str1);
            $value = $arr[0];
        }
        $data = $newTable;
        // 查询 数据库
        $where['how_type'] = 2;
        $saveData = [];
        for ($i = 0;$i<10;$i++){
            $saveData[$i] = [
                'howid' => $i +1+10,
                'how_type' => 2,
                'how_words' => $data[$i],
                'sort' => $i+1,
                'url' => 'https://s.weibo.com/weibo?q='.$data[$i],
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s'),
            ];
        }
        $model = M('udc_out_hotwords');
        if ($model->where($where)->select()){
            for ($j = 0;$j<10;$j++) {
                $where['howid'] = $j +1+10;
                unset($saveData[$j]['howid']);
                $model->where($where)->save($saveData[$j]); // 根据条件更新记录
            }
        }else{
            $model->addAll($saveData);
        }
        Log::write($model->getError());
    }


    private function getBaiduHotKeyWord()
    {
        $templateRss = file_get_contents('http://top.baidu.com/rss_xml.php?p=top10');
        If (preg_match('/<table>(.*)<\/table>/is', $templateRss, $_description)) {
            $templateRss = $_description [0];
            $templateRss = str_replace("&", "&amp;", $templateRss);
        }
        $templateRss = "<?xml version=\"1.0\" encoding=\"GBK\"?>" . $templateRss;
        $xml = simplexml_load_String($templateRss);
        foreach ($xml->tbody->tr as $temp) {
            if (!empty ($temp->td->a)) {
                $keyArray [] = trim(($temp->td->a));
            }
        }
        return $keyArray;
    }



    private function getUrlContent($url){//通过url获取html内容
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1 )");
        curl_setopt($ch,CURLOPT_HEADER,1);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }


    private function getTable($html) {
        preg_match_all("/<table>[\s\S]*?<\/table>/i",$html,$table);
        $table = $table[0][0];
        $table = preg_replace("'<table[^>]*?>'si","",$table);
        $table = preg_replace("'<tr[^>]*?>'si","",$table);
        $table = preg_replace("'<td[^>]*?>'si","",$table);
        $table = str_replace("</tr>","{tr}",$table);
        $table = str_replace("</td>","{td}",$table);
        //去掉 HTML 标记
        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
        //去掉空白字符
        $table = preg_replace("'([rn])[s]+'","",$table);
        $table = str_replace(" ","",$table);
        $table = str_replace(" ","",$table);
        $table = explode('{tr}', $table);
        array_pop($table);
        foreach ($table as $key=>$tr) {
            // 自己可添加对应的替换
            $tr = str_replace("\n\n","",$tr);
            $td = explode('{td}', $tr);
            array_pop($td);
            $td_array[] = $td;
        }
        return $td_array;
    }

    private function getSouGouHtml($html){
        preg_match_all("/<ul class=\"pub-list\">[\s\S]*?<\/ul>/i",$html,$table);
        $table = $table[0][0];
        $table = preg_replace("'<ul[^>]*?>'si","",$table);
        $table = preg_replace("'<li[^>]*?>'si","",$table);
        $table = preg_replace("'<span[^>]*?>'si","",$table);
        $table = str_replace("</li>","{tr}",$table);
        $table = str_replace("</span>","{td}",$table);
        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
        //去掉空白字符
        $table = preg_replace("'([rn])[s]+'","",$table);
        $table = str_replace(" ","",$table);
        $table = str_replace(" ","",$table);
        $table = explode('{tr}', $table);
        array_pop($table);
        foreach ($table as $key=>$tr) {
            // 自己可添加对应的替换
            $tr = str_replace("\n\n","",$tr);
            $td = explode('{td}', $tr);
            array_pop($td);
            $td_array[] = $td;
        }
        return $td_array;
}

  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章      下一篇文章      查看所有文章
加:2021-07-11 16:27:06  更:2021-07-11 16:27:28 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 6:18:59-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码