获取微博排行榜PHP

获取微博排行榜是获取微博html页面的数据,而非直接调用微博后端接口获取

PHP实现

php 复制代码
class WeiBoHotSearchService extends BaseService
{
    /**
     * 微博热搜缓存过期时间
     * @var int
     */
    protected int $expireTime = 600;

    /**
     * 微博热搜URL
     * @var string
     */
    protected string $domainUrl = 'https://s.weibo.com';

    /**
     * URL内容
     * @var mixed|null
     */
    protected mixed $output = null;


    /**
     * 热搜列表
     * @var array
     */
    protected array $hotSearchList = [];

    /**
     * 缓存key
     * @var string
     */
    protected string $key = 'WEIBO_HOT_SEARCH_LIST_CACHE';

    /**
     * 微博热搜Cookie
     * @var string
     */
    protected string $cookie = 'SUB=1;_s_tentry=weibo.com;';


    /**
     * 微博热搜url
     * @var string
     */
    protected string $url = 'https://s.weibo.com/top/summary?cate=realtimehot';

    /**
     * 获取微博热搜列表缓存
     * @return array|array[]|mixed
     */
    public function getWeiBoHotSearchListCache()
    {
        $str = RedisUtil::getInstance()->get($this->key);
        if (!empty($str)) {
            return json_decode($str, true);
        } else {
            return $this->refreshWeiboHotSearch();
        }
    }

    /**
     * 刷新微博热搜列表缓存
     * @return array|array[]
     */
    public function refreshWeiboHotSearch()
    {
        try {
            $output = HttpUtil::getInstance()->getUrlContent($this->url, $this->cookie);
            // 获取html元素中table表格
            preg_match_all('/<table>[\s\S]*?<\/table>/i', $output, $match);
            // 获取table表格
            $table = $match[0][0];
            // 去除table标签
            $table = preg_replace("'<table[^>]*?>'si", "", $table);
            // 去除tr标签
            $table = preg_replace("'<tr[^>]*?>'si", "", $table);
            // 去除td标签
            $table = preg_replace("'<td[^>]*?>'si", "", $table);
            // 将tr标签替换为{tr}
            $table = str_replace("</tr>", "{tr}", $table);
            // 将td标签替换为{td}
            $table = str_replace("</td>", "{td}", $table);
            //去掉剩余HTML元素并将<a>标签保留是为了拿到跳转链接
            $table = preg_replace("/<(?!a\b|\/a\b)[^>]*>/i", "", $table);
            //去掉空白字符
            $table = preg_replace("'([rn])[s]+'", "", $table);
            // 去除空格
//        $table = str_replace(" ", "", $table);
            // 将table标签按照{tr}拆分
            $table = explode('{tr}', $table);
            foreach ($table as $key => $value) {
                // 自己可添加对应的替换
                $tr = str_replace("\n", "", $value);
                // 去除数组中的<!--{td}-->
                $tr = preg_replace("<!--{td}-->", "", $tr);
                $td = explode('{td}', $tr);
                $td_array[] = $td;
            }
            // 删除数组第一项,删除数组最后一项
            array_shift($td_array);
            array_pop($td_array);
            // 处理数组
            $this->hotSearchList = array_map(function ($item) {
                // 第二项
                $item[1] = get_href_content_dom($item[1],$this->domainUrl);
                $item[2] = trim($item[2]);
                // 将二三项合并
                $item[1]['tip'] = $item[2];
                return $item[1];
            }, $td_array);
            Log::record("微博热搜获取数据成功" . json_encode($this->hotSearchList), 'info');
        } catch (Exception $e) {
            Log::record("微博热搜获取数据异常" . $e->getMessage(), 'error');
        }
        RedisUtil::getInstance()->set($this->key, json_encode($this->hotSearchList), $this->expireTime);
        return $this->hotSearchList;
    }
}

上面的RedisUtil就是一个工具类,其中只用了String类型的set和get方法

下期使用Python实现

相关推荐
学传打活4 天前
【边打字.边学昆仑正义文化】_进阶篇5 宇宙星系的运转(续3)
微信公众平台·1024程序员节·汉字·昆仑正义文化
学传打活10 天前
【边打字.边学昆仑正义文化】_进阶篇5 宇宙星系的运转(续1)
微信公众平台·1024程序员节·汉字·昆仑正义文化
fangzhanpeng16818 天前
快速掌握C#语言基础知识点(1.基础数据类型)
开发语言·c#·1024程序员节
无己心20 天前
面向多平台 AI 搜索引擎的适配层架构设计
前端·人工智能·搜索引擎·ai·1024程序员节
u0103055271 个月前
AI驱动的安卓UI自动生成功能实现
人工智能·1024程序员节
u0103055271 个月前
长株潭智能体赋能腾讯元器链接共享
1024程序员节
云贝贝贝1 个月前
OceanBase认证体系介绍与备考指南
运维·服务器·oceanbase·1024程序员节
u0103055271 个月前
Java图像处理实战指南
人工智能·1024程序员节
u0103055271 个月前
Java AWT鼠标事件全解析
人工智能·1024程序员节
u0103055271 个月前
Java实用类应用精讲
人工智能·1024程序员节