Extracting Comment Count from Page Title
Yahoo News comments page shows total comment count in the page title.
Page Title Format
(123)件のコメント - Yahoo!ニュース
Pattern: (NUMBER)件のコメント
Extraction Method
const commentData = await page.evaluate(() => {
let totalComments = 0;
const titleMatch = document.title.match(/(\d+)件のコメント/);
if (titleMatch) {
totalComments = parseInt(titleMatch[1], 10);
}
return { totalComments };
});
Regex Breakdown
(\d+)- Captures one or more digits件のコメント- Matches "件のコメント" (counter word for comments)
Complete Example
function extractCommentCount(pageTitle: string): number {
const match = pageTitle.match(/(\d+)件のコメント/);
return match ? parseInt(match[1], 10) : 0;
}
// Usage
const totalComments = extractCommentCount(document.title);
console.log(totalComments); // 123
Notes
- Returns 0 if no match found
- Japanese counter word "件" is used for comments
- Page title is the most reliable source for total count
- Individual comment extraction may return fewer comments (pagination limits)