Files
Yohan fd116fe4bc fix(xiaohongshu): scope note interaction selectors to .interact-container (#839)
* fix(xiaohongshu): scope note interaction selectors to .interact-container

The .like-wrapper / .collect-wrapper / .chat-wrapper class names are
also used by every comment's like/reply buttons in the comment section.
querySelector returned the FIRST match — which on a note with comments
is a comment's count, not the post's. As a result, `xiaohongshu note`
returned wrong like/collect/comment counts for any note that had user
comments.

Scoping each selector to .interact-container (the post's main
interaction bar) returns the correct post-level counts.

Verified on multiple notes:
- Note A: was returning likes=2, now correctly returns likes=74
- Note B: was returning likes=1, now correctly returns likes=796
- Note C: was returning likes=1, now correctly returns likes=269

* test(xiaohongshu): add regression check for .interact-container selector scope

Verify the evaluate script passes scoped selectors so unscoped
versions can't silently regress. Follows reviewer suggestion to
assert on page.evaluate.mock.calls[0][0].

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-08 14:18:44 +08:00

106 lines
4.2 KiB
TypeScript

/**
* Xiaohongshu note — read full note content from a public note page.
*
* Extracts title, author, description text, and engagement metrics
* (likes, collects, comment count) via DOM extraction.
*
* Supports both bare note IDs and full URLs (with xsec_token).
* Bare IDs now use /search_result/<id> which works without xsec_token
* when the user is logged in via cookies.
*/
import { cli, Strategy } from '@jackwener/opencli/registry';
import { AuthRequiredError, EmptyResultError } from '@jackwener/opencli/errors';
import { parseNoteId, buildNoteUrl } from './note-helpers.js';
cli({
site: 'xiaohongshu',
name: 'note',
description: '获取小红书笔记正文和互动数据',
domain: 'www.xiaohongshu.com',
strategy: Strategy.COOKIE,
args: [
{ name: 'note-id', required: true, positional: true, help: 'Note ID or full URL (preserves xsec_token for access)' },
],
columns: ['field', 'value'],
func: async (page, kwargs) => {
const raw = String(kwargs['note-id']);
const noteId = parseNoteId(raw);
const url = buildNoteUrl(raw);
await page.goto(url);
await page.wait(3);
const data = await page.evaluate(`
(() => {
const loginWall = /登录后查看|请登录/.test(document.body.innerText || '')
const notFound = /页面不见了|笔记不存在|无法浏览/.test(document.body.innerText || '')
const clean = (el) => (el?.textContent || '').replace(/\\s+/g, ' ').trim()
const title = clean(document.querySelector('#detail-title, .title'))
const desc = clean(document.querySelector('#detail-desc, .desc, .note-text'))
const author = clean(document.querySelector('.username, .author-wrapper .name'))
// Scope to .interact-container — the post's main interaction bar.
// Without scoping, .like-wrapper / .chat-wrapper also match each
// comment's like/reply buttons in the comment section, and
// querySelector returns the FIRST match (a comment's count, not the
// post's). The post's true counts live inside .interact-container.
const likes = clean(document.querySelector('.interact-container .like-wrapper .count'))
const collects = clean(document.querySelector('.interact-container .collect-wrapper .count'))
const comments = clean(document.querySelector('.interact-container .chat-wrapper .count'))
// Try to extract tags/topics
const tags = []
document.querySelectorAll('#detail-desc a.tag, #detail-desc a[href*="search_result"]').forEach(el => {
const t = (el.textContent || '').trim()
if (t) tags.push(t)
})
return { loginWall, notFound, title, desc, author, likes, collects, comments, tags }
})()
`);
if (!data || typeof data !== 'object') {
throw new EmptyResultError('xiaohongshu/note', 'Unexpected evaluate response');
}
if ((data as any).loginWall) {
throw new AuthRequiredError('www.xiaohongshu.com', 'Note content requires login');
}
if ((data as any).notFound) {
throw new EmptyResultError('xiaohongshu/note', `Note ${noteId} not found or unavailable — it may have been deleted or restricted`);
}
const d = data as any;
// XHS renders placeholder text like "赞"/"收藏"/"评论" when count is 0;
// normalize to '0' unless the value looks numeric.
const numOrZero = (v: string) => /^\d+/.test(v) ? v : '0';
// Title + author are always present on a real note page.
// If both are missing, the page likely failed to load properly.
if (!d.title && !d.author) {
throw new EmptyResultError(
'xiaohongshu/note',
'The note page loaded without visible content. The note may be deleted or restricted.',
);
}
const rows = [
{ field: 'title', value: d.title || '' },
{ field: 'author', value: d.author || '' },
{ field: 'content', value: d.desc || '' },
{ field: 'likes', value: numOrZero(d.likes || '') },
{ field: 'collects', value: numOrZero(d.collects || '') },
{ field: 'comments', value: numOrZero(d.comments || '') },
];
if (d.tags?.length) {
rows.push({ field: 'tags', value: d.tags.join(', ') });
}
return rows;
},
});