Open tiancheng91 opened 6 years ago
npm EACCES 错误
https://docs.npmjs.com/getting-started/fixing-npm-permissions
// 不知道什么鬼,换老版本npm没问题 npm i npm@4 -g
链式调用
// 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();
})();
ubuntu 18.04 部分环境异常
chrome --headless --no-sandbox --disable-setuid-sandbox --disable-gpu --disable-dev-shm-usage
抛出 Lost UI shared context
, 无法加载页面
puppeteer 过滤脚本
page.on('request', async request => {
if (request.url().indexOf('googleadservices.com') > 0) {
request.abort();
}
request.continue();
});
多会话处理: https://github.com/GoogleChrome/puppeteer/issues/85 https://github.com/GoogleChrome/puppeteer/issues/645
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();
})();
安装
troubleshooting
[推荐]snap安装
snap install chromium && apt install fonts-wqy-microhei
官方deb包安装
docker启动
puppeteer
使用
puppent启动
headless中使用的默认参数