Parsing Comment Reactions from Yahoo News
Yahoo News comments have three reaction types with counts displayed in button text.
Reaction Types
| Japanese | English | Button Text Pattern |
|---|---|---|
| 共感した | Empathy | 共感した\s*(\d+) |
| なるほど | Insightful | なるほど\s*(\d+) |
| うーん | Disagree | うーん\s*(\d+) |
Extraction Method
const reactions: { empathy?: number; insightful?: number; disagree?: number } = {};
const reactionButtons = commentArticle.querySelectorAll('button');
reactionButtons.forEach((btn) => {
const text = btn.textContent?.trim() || '';
const empathyMatch = text.match(/共感した\s*(\d+)/);
if (empathyMatch) {
reactions.empathy = parseInt(empathyMatch[1], 10);
}
const insightfulMatch = text.match(/なるほど\s*(\d+)/);
if (insightfulMatch) {
reactions.insightful = parseInt(insightfulMatch[1], 10);
}
const disagreeMatch = text.match(/うーん\s*(\d+)/);
if (disagreeMatch) {
reactions.disagree = parseInt(disagreeMatch[1], 10);
}
});
Output Structure
Only include reactions that have counts:
{
empathy: 15,
insightful: 8,
disagree: 2
}
Or undefined if no reactions:
const reactions = Object.keys(reactions).length > 0 ? reactions : undefined;
Integration with Comment Extraction
const extractedComments = [];
commentItems.forEach((item) => {
// ... extract username, text ...
// Extract reactions
const reactions: any = {};
const reactionButtons = commentArticle.querySelectorAll('button');
reactionButtons.forEach((btn) => {
const text = btn.textContent?.trim() || '';
if (text.includes('共感した')) {
reactions.empathy = parseInt(text.match(/\d+/)?.[0] || '0', 10);
}
// ... other reactions
});
extractedComments.push({
text: commentText,
username: username || undefined,
reactions: Object.keys(reactions).length > 0 ? reactions : undefined,
});
});