-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chrome headless #1
Comments
npm EACCES 错误 https://docs.npmjs.com/getting-started/fixing-npm-permissions // 不知道什么鬼,换老版本npm没问题 |
链式调用 // pup-chain
const ObjProperties = require("simple-property-retriever");
const pWaterfall = require('p-waterfall');
class Chain {
constructor(initiator) {
this.initiated = true; // idk, I just put it here to track things. Dead code for now.
this.chain = []; // the container for all chains
let addMethods = (ref) => {
this.ref = ref;
const properties = ObjProperties.getOwnNonenumerables(ref.__proto__);
for (const property of properties) {
// if (property === "constructor")
// continue;
this[property] = function(...args) {
this.chain.push(() => this.ref[property](...args))
return this;
};
};
return this.ref;
}
addMethods(initiator)
}
run() {
let chain = [...this.chain];
this.chain = [];
return pWaterfall(chain)
}
};
module.exports = Chain; // app.js
const puppeteer = require('puppeteer');
const Chain = require("pup-chain");
(async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
let urls = ["https://example.com", "https://example.org"];
for (url of urls) {
let mainChain = await new Chain(page);
newurl = await mainChain
.goto(url)
.url()
.run();
title = await mainChain
.title()
.run();
console.log(`${url} > URL: "${newurl}", Title: "${title}"`);
}
browser.close();
})(); |
This comment has been minimized.
This comment has been minimized.
需求: DevTools proxy
|
puppeteer 过滤脚本 page.on('request', async request => {
if (request.url().indexOf('googleadservices.com') > 0) {
request.abort();
}
request.continue();
}); |
多会话处理: const puppeteer = require('puppeteer');
const Page = require('puppeteer/lib/Page');
async function newPageWithNewContext(browser) {
const {browserContextId} = await browser._connection.send('Target.createBrowserContext');
const {targetId} = await browser._connection.send('Target.createTarget', {url: 'about:blank', browserContextId});
const client = await browser._connection.createSession(targetId);
const page = await Page.create(client, browser._ignoreHTTPSErrors, browser._screenshotTaskQueue);
page.browserContextId = browserContextId;
return page;
}
async function closePage(browser, page) {
if (page.browserContextId != undefined) {
await browser._connection.send('Target.disposeBrowserContext', {browserContextId: page.browserContextId});
}
await page.close();
}
(async () => {
const browser = await puppeteer.launch();
const page = await newPageWithNewContext(browser);
await page.goto('https://example.com');
console.log(await page.cookies());
await closePage(browser, page);
await browser.close();
})(); |
Open
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
安装
troubleshooting
[推荐]snap安装
snap install chromium && apt install fonts-wqy-microhei
官方deb包安装
docker启动
puppeteer
使用
####puppent启动
headless中使用的默认参数
The text was updated successfully, but these errors were encountered: