我试图使用puppeteer抓取Instagram视频的视频url,但无法做到。它返回null作为响应
这是我的代码
async function getVideo(){
const launch = await puppeteer.launch({headless: true});
const page = await launch.newPage();
await page.goto('https://www.instagram.com/p/CfW5u5UJmny/?hl=en');
const video = await page.evaluate(() => {
return document.querySelector('video').src;
});
console.log(video); returns null
await launch.close();
}
你的例子:https://instagram.fluh1-1.fna.fbcdn.net/v/t50.16885-16/290072800_730588251588660_5005285215058589375_n.mp4?efg=eyJ2ZW5jb2RlX3RhZyI6InZ0c192b2RfdXJsZ2VuLjcyMC5pZ3R2LmJhc2VsaW5lIiwicWVfZ3JvdXBzIjoiW1wiaWdfd2ViX2RlbGl2ZXJ5X3Z0c19vdGZcIl0ifQ&_nc_ht=instagram.fluh1-1.fna.fbcdn.net&_nc_cat=100&_nc_ohc=ROJWkaOqkQcAX_z-_Ls&edm=AP_V10EBAAAA&vs=440468611258459_2442386419&_nc_vs=HBksFQAYJEdPQW9TaEUwaURaVmQ1Z0NBTC0yRkV0aVdIWkZidlZCQUFBRhUAAsgBABUAGCRHTEdvVHhGMWFjUUpsMzhDQUZNT0c1cV8wT3 c1ynzwqkfbquyvagliaqaoabgagwgib3vzzv9vawwbmruaacaa % 2 bo % 2 fynlpeqbuckajdmywxqcddsi0ovgqyemrhc2hfymfzzwxpbmvfmv92mreadewhaa % 3 d % 3 d&建行= 7-5&哦= 00 _afcbracqlxoqmbgswrk_6urv_fmhjufdit-8w6eo0_uchq& oe = 638 d6cbd& _nc_sid = 4 f375e
您正在加载Instagram页面。由于加载需要一段时间,所以我使用了setTimeout函数来等待。Puppeteer也有许多内置函数可以用来获取src,例如:
async function getVideo(){
const launch = await puppeteer.launch({headless: false});
const page = await launch.newPage();
await page.goto('https://www.instagram.com/p/CfW5u5UJmny/?hl=en');
setTimeout(async () => {
let src = await page.$eval("video", n => n.getAttribute("src"))
console.log(src);
await launch.close();
}, 1000)
}