Skip to content

eventstream捕获到了,但是没法决定等待时间!!! #645

@123456789TXZ

Description

@123456789TXZ

网站是:百度搜索ai文心助手
https://chat.baidu.com/search?isShowHello=1&extParams=%7B%22out_enter_type%22%3A%22home_aiinput_askai%22%2C%22enter_type%22%3A%22sidebar_dialog%22%7D

Image Image

但是响应里面没有这个stream

获取内容.js:
(function () {
/***********************
* 0️⃣ fetch 防重复 hook
***********************/
if (window.raw_fetch) {
window.fetch = window.raw_fetch;
} else {
window.raw_fetch = window.fetch;
}

/***********************
 * 1️⃣ 创建 / 清空左侧面板
 ***********************/
let box = document.getElementById("__sse_monitor__");
if (!box) {
    box = document.createElement("div");
    box.id = "__sse_monitor__";
    box.style.cssText = `
        position: fixed;
        left: 150px;
        top: 120px;
        width: 160px;
        height: 80vh;
        overflow-y: auto;
        background: #0b0f14;
        color: #9efc9e;
        font-size: 12px;
        font-family: Consolas, monospace;
        border: 1px solid #1f2937;
        border-radius: 6px;
        box-shadow: 0 0 10px rgba(0,0,0,.6);
        z-index: 999999;
        padding: 8px;
        box-sizing: border-box;
    `;
    document.body.appendChild(box);
} else {
    box.innerHTML = "";
}

/***********************
 * 1️⃣-1 标题
 ***********************/
const title = document.createElement("div");
title.textContent = "Baidu AI SSE Monitor";
title.style.cssText = `
    font-weight: bold;
    margin-bottom: 6px;
    color: #60a5fa;
`;
box.appendChild(title);

/***********************
 * 2️⃣ 写入一条 SSE 消息
 ***********************/
function appendMsg({ url, event, id, data, done }) {
    const div = document.createElement("div");
    div.className = "sse-msg";

    // —— 结构化数据(给 XPath / 自动化用)——
    div.setAttribute("data-url", url || "");
    div.setAttribute("data-event", event || "");
    div.setAttribute("data-id", id || "");
    div.setAttribute("data-data", data || "");
    div.setAttribute("data-done", done ? "true" : "false");

    // —— 页面可视文本 ——
    div.textContent = done ? "[DONE]" : data;

    div.style.cssText = `
        padding: 4px 2px;
        border-bottom: 1px dashed #1f2937;
        white-space: pre-wrap;
        word-break: break-all;
    `;

    box.appendChild(div);
    box.scrollTop = box.scrollHeight;
}

/***********************
 * 3️⃣ 解析 SSE block
 ***********************/
function parseSSEBlock(block) {
    const msg = { event: "", id: "", data: "" };

    block.split("\n").forEach(line => {
        if (line.startsWith("event:")) {
            msg.event = line.slice(6).trim();
        } else if (line.startsWith("id:")) {
            msg.id = line.slice(3).trim();
        } else if (line.startsWith("data:")) {
            msg.data += line.slice(5).trim();
        }
    });

    return msg;
}

/***********************
 * 4️⃣ hook fetch(只抓百度对话接口)
 ***********************/
const _fetch = window.fetch;
window.fetch = async (...args) => {
    const [input, init = {}] = args;

    const url =
        typeof input === "string"
            ? input
            : input?.url || "";

    const method =
        init.method ||
        (input instanceof Request ? input.method : "GET");

    // ❌ 非目标接口直接放行
    if (
        !url.startsWith("https://chat.baidu.com/aichat/api/conversation") ||
        method.toUpperCase() !== "POST"
    ) {
        return _fetch(...args);
    }

    const res = await _fetch(...args);

    const ct = res.headers.get("content-type") || "";
    if (!ct.includes("text/event-stream")) {
        return res;
    }

    // 标记命中
    appendMsg({
        url,
        data: "【MATCHED Baidu SSE】",
        done: false
    });

    const reader = res.clone().body.getReader();
    const decoder = new TextDecoder("utf-8");
    let buffer = "";

    while (true) {
        const { value, done } = await reader.read();
        if (done) break;

        buffer += decoder.decode(value, { stream: true });
        const blocks = buffer.split("\n\n");
        buffer = blocks.pop();

        for (const block of blocks) {
            const msg = parseSSEBlock(block);

            if (msg.data === "[DONE]") {
                appendMsg({
                    url,
                    data: "[DONE]",
                    done: true
                });
                continue;
            }

            if (msg.data) {
                appendMsg({
                    url,
                    event: msg.event,
                    id: msg.id,
                    data: msg.data,
                    done: false
                });
            }
        }
    }

    return res;
};

})();

我就写了一个js抓这个。 page.run_js('获取内容.js')运行

但是没法智能决定等待时间
page.run_js('获取内容.js')
content = f'123'
l1 = page.ele('#chat-input-box')

l1.input(f'{content}')
wait_time2 = random.uniform(1, 2)  # 在 1 到 2 秒之间随机选择
l1.input('\n')


time.sleep(20)
bb=page.html

只能一律写成20s,但是效率就慢了

现在问题是如何解决智能等待的,或者把这个js加到drissionpage里面,应为drissionpage监听响应就是监听完再进行下一步,这个原理是我想实现的

@g1879 麻烦作者看看

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions