| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- """暴力提取:从window.__INITIAL_STATE__或Vue根组件拿笔记ID"""
- import json, time, websocket, urllib.request
- tabs = json.loads(urllib.request.urlopen("http://127.0.0.1:9222/json").read())
- ws_url = None
- for t in tabs:
- if "creator.xiaohongshu.com" in t.get("url", ""):
- ws_url = t["webSocketDebuggerUrl"]
- break
- ws = websocket.create_connection(ws_url, timeout=15)
- msg_id = 0
- def send_cmd(method, params=None):
- global msg_id
- msg_id += 1
- ws.send(json.dumps({"id": msg_id, "method": method, "params": params or {}}))
- while True:
- resp = json.loads(ws.recv())
- if resp.get("id") == msg_id:
- return resp.get("result", {})
- send_cmd("Page.navigate", {"url": "https://creator.xiaohongshu.com/new/note-manager"})
- time.sleep(6)
- # 暴力搜索window对象中所有包含noteId的属性
- js = """
- (function() {
- var results = [];
-
- // 搜索window上的全局状态
- var keys = ['__INITIAL_STATE__', '__NUXT__', '__APP_DATA__', '__PRELOADED_STATE__'];
- for (var i = 0; i < keys.length; i++) {
- if (window[keys[i]]) {
- results.push({source: keys[i], type: typeof window[keys[i]]});
- }
- }
-
- // 搜索Vue实例
- var app = document.querySelector('#app') || document.querySelector('[id]');
- if (app && app.__vue__) {
- results.push({source: 'vue_root', type: 'found'});
- // 遍历vue data
- try {
- var vueData = JSON.stringify(app.__vue__.$data || {}).substring(0, 2000);
- results.push({source: 'vue_data', data: vueData});
- } catch(e) {
- results.push({source: 'vue_data_error', msg: e.message});
- }
- }
-
- // 搜索所有__vue__实例
- var vueEls = document.querySelectorAll('*');
- var vueCount = 0;
- for (var j = 0; j < vueEls.length && vueCount < 3; j++) {
- if (vueEls[j].__vue__) {
- vueCount++;
- try {
- var d = vueEls[j].__vue__.$data;
- var str = JSON.stringify(d);
- if (str.indexOf('noteId') >= 0 || str.indexOf('note_id') >= 0) {
- results.push({source: 'vue_with_noteId', data: str.substring(0, 3000)});
- }
- } catch(e) {}
- }
- }
-
- // 搜索React root
- var rootEl = document.getElementById('root') || document.getElementById('app');
- if (rootEl) {
- var rKey = Object.keys(rootEl).find(function(k) { return k.startsWith('__reactContainer') || k.startsWith('__reactFiber'); });
- if (rKey) {
- results.push({source: 'react_root', key: rKey});
- }
- }
-
- // 最后手段:搜索所有script标签中的JSON数据
- document.querySelectorAll('script').forEach(function(s) {
- var text = s.textContent || '';
- if (text.indexOf('noteId') >= 0 || text.indexOf('note_id') >= 0) {
- var match = text.match(/"noteId"\s*:\s*"([a-f0-9]+)"/g);
- if (match) {
- results.push({source: 'script_tag', noteIds: match.slice(0, 10)});
- }
- }
- });
-
- return JSON.stringify(results, null, 2);
- })()
- """
- result = send_cmd("Runtime.evaluate", {"expression": js, "returnByValue": True})
- val = result.get("result", {}).get("value", "")
- with open(r"C:\code\cfc\运营文案\小红书发布\_process_scripts\global_state.txt", "w", encoding="utf-8") as f:
- f.write(val)
- print("DONE")
- ws.close()
|