Spaces:
Sleeping
Sleeping
Update scraper.js
Browse files- scraper.js +34 -32
scraper.js
CHANGED
|
@@ -15,44 +15,46 @@ async function getPinterestVideos(query) {
|
|
| 15 |
const searchUrl = `https://www.pinterest.com/search/pins/?q=${encodeURIComponent(query)}&rs=typed`;
|
| 16 |
await page.goto(searchUrl, { waitUntil: 'networkidle2', timeout: 30000 });
|
| 17 |
|
| 18 |
-
//
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
const pinUrls = await page.evaluate(() => {
|
| 25 |
-
|
| 26 |
-
|
| 27 |
});
|
| 28 |
|
| 29 |
-
|
| 30 |
-
const
|
| 31 |
-
console.error(`
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
}
|
| 50 |
}
|
| 51 |
-
}
|
| 52 |
-
})
|
|
|
|
| 53 |
}
|
| 54 |
-
} catch (
|
| 55 |
-
console.error(
|
| 56 |
} finally {
|
| 57 |
if (browser) await browser.close();
|
| 58 |
}
|
|
|
|
| 15 |
const searchUrl = `https://www.pinterest.com/search/pins/?q=${encodeURIComponent(query)}&rs=typed`;
|
| 16 |
await page.goto(searchUrl, { waitUntil: 'networkidle2', timeout: 30000 });
|
| 17 |
|
| 18 |
+
// --- SCROLLING LOGIC TO FIND MORE PINS ---
|
| 19 |
+
await page.evaluate(async () => {
|
| 20 |
+
for (let i = 0; i < 3; i++) { // Scroll 3 times to load ~60 pins
|
| 21 |
+
window.scrollBy(0, window.innerHeight * 2);
|
| 22 |
+
await new Promise(r => setTimeout(r, 1500));
|
| 23 |
+
}
|
| 24 |
+
});
|
| 25 |
|
| 26 |
const pinUrls = await page.evaluate(() => {
|
| 27 |
+
return Array.from(document.querySelectorAll('a[href*="/pin/"]'))
|
| 28 |
+
.map(a => a.href).filter(href => href.includes('/pin/'));
|
| 29 |
});
|
| 30 |
|
| 31 |
+
// Get up to 60 unique pins to check
|
| 32 |
+
const uniquePins = [...new Set(pinUrls)].slice(0, 60);
|
| 33 |
+
console.error(`Found ${uniquePins.length} potential pins. Checking for video files...`);
|
| 34 |
+
|
| 35 |
+
for (const pinUrl of uniquePins) {
|
| 36 |
+
const pPage = await browser.newPage();
|
| 37 |
+
try {
|
| 38 |
+
// We go to each pin and try to find the video source
|
| 39 |
+
await pPage.goto(pinUrl, { waitUntil: 'domcontentloaded', timeout: 10000 });
|
| 40 |
+
const content = await pPage.content();
|
| 41 |
+
const videoRegex = /https:\/\/v1\.pinimg\.com\/videos\/mc\/[^\s"']+/g;
|
| 42 |
+
const matches = content.match(videoRegex);
|
| 43 |
+
|
| 44 |
+
if (matches) {
|
| 45 |
+
let rawUrl = matches[0].replace(/\\u002F/g, '/').replace(/[奖励"']/g, '');
|
| 46 |
+
const hashMatch = rawUrl.match(/([a-f0-9]{32})/);
|
| 47 |
+
if (hashMatch) {
|
| 48 |
+
const h = hashMatch[1];
|
| 49 |
+
// Output the 720p URL for Python to check
|
| 50 |
+
process.stdout.write(`https://v1.pinimg.com/videos/mc/720p/${h.substring(0,2)}/${h.substring(2,4)}/${h.substring(4,6)}/${h}.mp4\n`);
|
|
|
|
| 51 |
}
|
| 52 |
+
}
|
| 53 |
+
} catch (e) {}
|
| 54 |
+
await pPage.close();
|
| 55 |
}
|
| 56 |
+
} catch (error) {
|
| 57 |
+
console.error("Scraper Error: " + error.message);
|
| 58 |
} finally {
|
| 59 |
if (browser) await browser.close();
|
| 60 |
}
|