Created
April 20, 2021 13:42
-
-
Save xettri/7b45c5aaefbbd8eaf759cb9cc78f6357 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require("puppeteer"); | |
async function request(url, cookie, headers = {}, cb) { | |
try { | |
const browser = await puppeteer.launch({ args: ["--start-fullscreen"] }); | |
const page = await browser.newPage(); | |
if (cookie) { | |
await page.setCookie(cookie); | |
} | |
await page.setViewport({ width: 1366, height: 768 }); | |
await page.setExtraHTTPHeaders({ | |
"user-agent": | |
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36", | |
...headers | |
}); | |
await page.goto(url, { | |
waitUntil: "networkidle0", | |
}); | |
// if you need screenshot when site loaded | |
await page.screenshot({ path: "example.png" }); | |
const html = await page.evaluate( | |
() => document.documentElement.outerHTML | |
); | |
await browser.close(); | |
cb && cb(null, html); | |
} catch (err) { | |
console.error(err); | |
cb && cb(error); | |
} | |
} | |
request("any url", cookie, (error, html) => { | |
console.log(html); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment