Pin_Video_Reel / scraper.js
nazib61's picture
Update scraper.js
0cef924 verified
const puppeteer = require('puppeteer-core');
async function getPinterestVideos(query) {
let browser;
try {
browser = await puppeteer.launch({
executablePath: '/usr/bin/chromium',
headless: 'new',
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage']
});
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36');
const searchUrl = `https://www.pinterest.com/search/pins/?q=${encodeURIComponent(query)}&rs=typed`;
await page.goto(searchUrl, { waitUntil: 'networkidle2', timeout: 30000 });
await page.evaluate(async () => {
for (let i = 0; i < 3; i++) {
window.scrollBy(0, window.innerHeight * 2);
await new Promise(r => setTimeout(r, 1500));
}
});
const pinUrls = await page.evaluate(() => {
return Array.from(document.querySelectorAll('a[href*="/pin/"]'))
.map(a => a.href).filter(href => href.includes('/pin/'));
});
const uniquePins = [...new Set(pinUrls)].slice(0, 60);
for (const pinUrl of uniquePins) {
const pPage = await browser.newPage();
try {
await pPage.goto(pinUrl, { waitUntil: 'domcontentloaded', timeout: 10000 });
const content = await pPage.content();
const videoRegex = /https:\/\/v1\.pinimg\.com\/videos\/mc\/[^\s"']+/g;
const matches = content.match(videoRegex);
if (matches) {
let rawUrl = matches[0].replace(/\\u002F/g, '/').replace(/[奖励"']/g, '');
const hashMatch = rawUrl.match(/([a-f0-9]{32})/);
if (hashMatch) {
const h = hashMatch[1];
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`);
}
}
} catch (e) {}
await pPage.close();
}
} catch (error) {
process.stderr.write(error.message);
} finally {
if (browser) await browser.close();
}
}
getPinterestVideos(process.argv.slice(2).join(' '));