DailyHotApi icon indicating copy to clipboard operation
DailyHotApi copied to clipboard

微博热搜挂了

Open AssassinJY opened this issue 1 month ago • 2 comments

问题描述

疑似官方链接崩了

Image

补充信息

No response

AssassinJY avatar Nov 19 '25 06:11 AssassinJY

结论:加了cookie验证防爬api 可以手动设置一个SUB/SUBP键cookie,如我刚刚获取的SUB=_2AkMeQnWbf8NxqwFRmv0RyWnnbIl3yw_EieKoHoRAJRM3HRl-yT9yqlwetRB6NcJbdk7-tZAbJh6FI9H-ATAyJ1sFeMkk;SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9WWepoiVdnQ_hRyypCb.c4lY,可惜vercel那边不能加cookie毕竟是引入的npm

以下是过程 我也遇到了Cannot read properties of undefined (reading 'cards') 手动访问api返回code 432,随后api响应也变成Request failed with status code 432 测试带登录访问一次 https://m.weibo.cn/ 就好了 我感到很疑惑,为什么访问一次就好了 查看当时的返回结果

{
  ok: -100,
  url: 'https://passport.weibo.com/sso/signin?entry=wapsso&source=wapssowb&url=https%3A%2F%2Fs.weibo.com%2Ftop%2Fsummary%3Fcate%3Drealtimehot'
}

可能需要验证cookie了 实测,大概是这几种情况 如果没有referer也没有cookie,概率返回432,或者自动重定向到Sina Visitor System 如果Refererhttps://s.weibo.com/top/summary?cate=realtimehot会直接响应Sina Visitor Syste, 如果你还设置了X-Requested-WithXMLHttpRequest会返回以上-100 如果cookie过期返回432,此时清除cookie里的WEIBOCN_FROM=1110106030就会重新获取cookie了

Sina Visitor System的结果是post请求https://visitor.passport.weibo.cn/visitor/genvisitor2获取SUBSUBP的cookie Image 此时立马请求原api就会在api请求的同时setcookie,不过影响432的cookie还是只有SUB和SUBP部分 Image

xiaofeiTM233 avatar Nov 20 '25 11:11 xiaofeiTM233

点击展开/折叠代码
import type { RouterData } from "../types.js";
import type { RouterType } from "../router.types.js";
import { get } from "../utils/getData.js";
import { getTime } from "../utils/getTime.js";
import { config } from "../config";

export const handleRoute = async (_: undefined, noCache: boolean) => {
  const listData = await getList(noCache);
  const routeData: RouterData = {
    name: "weibo",
    title: "微博",
    type: "热搜榜",
    description: "实时热点,每分钟更新一次",
    link: "https://s.weibo.com/top/summary/",
    total: listData.data?.length || 0,
    ...listData,
  };
  return routeData;
};

const getList = async (noCache: boolean) => {
  const url = "https://m.weibo.cn/api/container/getIndex?containerid=106003type%3D25%26t%3D3%26disable_hot%3D1%26filter_type%3Drealtimehot&title=%E5%BE%AE%E5%8D%9A%E7%83%AD%E6%90%9C&extparam=filter_type%3Drealtimehot%26mi_cid%3D100103%26pos%3D0_0%26c_type%3D30%26display_time%3D1540538388&luicode=10000011&lfid=231583";
  const result = await get({
    url,
    noCache,
    ttl: 60,
    responseType: "json",
    headers: {
      Accept: "application/json,text/plain,*/*",
      "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
      "Cache-Control": "no-cache",
      Pragma: "no-cache",
      Referer: "https://s.weibo.com/top/summary?cate=realtimehot",
      "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36",
      Cookie: config.WEIBO_COOKIE ?? "SUBP=xxx...xxx",
    },
  });

  const rawGroup = (result?.data as any)?.data?.cards
    ?.find((c: any) => c.card_type === 11 && c.itemid === "hotword")
    ?.card_group ?? [];
	
  const data = rawGroup
    .slice(1)
    .filter((v: RouterType["weibo"]) => {
      if (
        v?.pic === "https://simg.s.weibo.com/20210408_search_point_orange.png"
      ) {
        return false;
      }
      return !!v?.desc;
    })
    .map((v: RouterType["weibo"]) => {
      const key = v.word_scheme ?? `#${v.desc}`;
      return {
        id: v.itemid,
        title: v.desc,
        desc: key,
        timestamp: getTime(v.onboard_time),
        hot: v.desc_extr,
        url: `https://s.weibo.com/weibo?q=${encodeURIComponent(
          key,
        )}&t=31&band_rank=1&Refer=top`,
        mobileUrl: v?.scheme,
      };
    });

  return {
    ...result,
    data,
  };
};
Image

https://s.weibo.com/top/summary?cate=realtimehothttps://m.weibo.cn/api/...https://weibo.com/ajax/side/hotSearch这几个接口加上Cookie都能用,还是等牢哥抓不用Cookie的接口吧

JianBing77 avatar Nov 22 '25 01:11 JianBing77

微博目前是可以直接查看热榜的概述的,但是,一旦想进入某个热搜里面查看内容的时候,就必须验证登录状态了,某乎目前什么东西都需要验证登录状态。 个人尝试了下,参照某乎的做法,在请求头中,设置 cookie 后,微博的 API 就可以了。各位可以自己试试,至于 cookie 敏感数据啥的,到时候出问题,别来找我。

first19326 avatar Nov 28 '25 02:11 first19326