Scraping WhatsApp -Web -Chats - Lazy Loading ProblemJavaScript

Javascript-Forum
Anonymous
 Scraping WhatsApp -Web -Chats - Lazy Loading Problem

Post by Anonymous »

Ich versuche WhatsApp -Webkontakte mit Dramatikern zu kratzen. Ziel ist es, durch die Chat -Liste zu scrollen und den Chat jedes Kontakts zu öffnen, um ihren Namen und seine Nummer aus dem Profilbereich zu extrahieren. Während Sie scrollen, werden zusätzliche Chats gerendert, aber die Gesamtzahl der sichtbaren Chats bleibt gleich. Verhalten Sie sich richtig - es ist, als würde der falsche Container gescrollt, oder die Seite löst nicht das faule Laden aus. await page.exposeFunction("scrapeContacts", async () => {
const scrapedContacts = new Set();
const contactList = [];

const scrollSelector = 'div[aria-label="Chat list"]';

let currentScroll = 0;
let targetScroll = await page.evaluate((sel) => {
const el = document.querySelector(sel);
return el?.scrollHeight || 0;
}, scrollSelector);

while (currentScroll < targetScroll ) {
const isRunning = await page.evaluate(() => window.scrapeRunning);
if (!isRunning) {
console.log("🛑 Scraping stopped by user.");
break;
}
// Refetch chat items (they change due to lazy loading)
const chatBoxes = await page.$$(
'#pane-side [aria-label="Chat list"] > div > div'
);

for (let i = 0; i < chatBoxes.length; i++) {
console.log(`Length: ${i}/${chatBoxes.length}`);
const chat = chatBoxes;

// Get height of the current chat
const chatHeight = await chat.evaluate((el) => el.offsetHeight);

try {
await chat.scrollIntoViewIfNeeded();
await chat.click();
await page.waitForTimeout(500);

await page.click('[title="Profile details"]');

// Skip groups/communities
const isGroup = await page.$(
'div[title="Group info"], div[title="Community info"]'
);
if (isGroup) {
console.log("⏭️ Skipping group or community chat");
await page.evaluate((scrollBy) => {
const el = document.querySelector("#pane-side");
if (el) el.scrollBy(0, scrollBy);
}, chatHeight);
continue;
}

await page.waitForSelector('div[title="Contact info"]', {
timeout: 5000,
});

const info = await page.evaluate(() => {
const nameEl = document.querySelector('header span[dir="auto"]');
const numberEl = document.querySelector(
'span[dir="auto"][class*="_ao3e"] > span'
);
const name = nameEl?.textContent || "";
const number = numberEl?.textContent || "";

return {
name: /^\+\d+/.test(name) ? "" : name,
number: /^\+\d+/.test(name) ? name : number,
};
});

const key = `${info.name}-${info.number}`;
if (!scrapedContacts.has(key) && info.number) {
scrapedContacts.add(key);
contactList.push(info);
console.log("✅ Collected:", info);
}
} catch (err) {
console.log("⚠️ Error on chat:", err.message);
}

// Scroll down by the height of the current chat
await page.evaluate((scrollBy) => {
const el = document.querySelector("#pane-side");
if (el) el.scrollBy(0, scrollBy);
}, chatHeight);

currentScroll += chatHeight;
}
await page.waitForTimeout(1000);
}
< /code>
Was ich ausprobiert habe: < /h2>
  • Scrolling #Pane-Side mit ScrollBy () < /li>
    Verwenden Sie bei jedem Chat. Liste nach jedem Scroll

Jede Hilfe mit einer besseren Strategie, um alle Kontakte von WhatsApp -Web mit Dramatikern zuverlässig zu scrollen und zu holen, würde geschätzt!>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post