自动化工具 谷歌官方推出了 Headless Chrome 的封装库 Puppeteer。感觉以后的浏览器自动化,都会基于这个库。

恒温 · 2017年08月19日 · 最后由 颜海镜 回复于 2018年03月22日 · 8991 次阅读

https://github.com/GoogleChrome/puppeteer

Puppeteer is a Node library which provides a high-level API to control headless Chrome over the DevTools Protocol. It can also be configured to use full (non-headless) Chrome.

以前用 phantomjs,现在可以用 Puppeteer,不知道这个库能维护多久。phantomjs 能做的事情,Puppeteer 都能做!

/**
 * Copyright 2017 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

const puppeteer = require('puppeteer');

(async() => {

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://google.com', {waitUntil: 'networkidle'});
// Type our query into the search bar
await page.type('puppeteer');

await page.click('input[type="submit"]');

// Wait for the results to show up
await page.waitForSelector('h3 a');

// Extract the results from the page
const links = await page.evaluate(() => {
  const anchors = Array.from(document.querySelectorAll('h3 a'));
  return anchors.map(anchor => anchor.textContent);
});
console.log(links.join('\n'));
browser.close();

})();
➜  puppeteer node search.js
puppeteerの意味 - 英和辞典 Weblio辞書
GitHub - GoogleChrome/puppeteer: Headless Chrome Node API
puppeteerの意味・用例|英辞郎 on the WEB:アルク
--headless時代の本命? Chrome を Node.jsから操作するライブラリ ...
Puppeteer - Wikipedia
Puppeteer (video game) - Wikipedia
puppeteer - ウィクショナリー日本語版
MAX - Puppeteer (OFFICIAL MUSIC VIDEO) - YouTube
Puppeteer: Headless Chrome Node API | Hacker News
Puppeteer™ Game | PS3 - PlayStation
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 12 条回复 时间 点赞

chrome 浏览器可以用它,但是其他家的浏览器也可以吗?👀

恒温 #12 · 2017年08月19日 Author
对 bauul 回复

不能,他启动了一个 chromium

@Lihuazhang 写了篇文章 https://testerhome.com/topics/9753,网站系统好像有些问题,访问不了。

markdown 有问题。这里报错不好。

对 恒温 回复

修复就好

PhantomJS 的作者已经说了. 要放弃 PhantomJS 的开发了. 因为 HeadLess Chrome 让他的项目失去意义了.

今天才看到这个库,落后了。。。

chrome firefox 都推出了各自的 headless 模式。 phantomjs 已经放弃了。

学习下

恒温 在 使用 selenium 爬取 测试银行卡需要的数据 中提及了此贴 10月31日 09:13

selenium 也能利用 headless 的,不一定要基于 Puppeteer

对 bauul 回复

selenium

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册。