UNPKG

1.83 kBJavaScriptView Raw
1/**
2 * Copyright 2017 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @fileoverview Search developers.google.com/web for articles tagged
19 * "Headless Chrome" and scrape results from the results page.
20 */
21
22'use strict';
23
24const puppeteer = require('puppeteer');
25
26(async() => {
27
28const browser = await puppeteer.launch();
29const page = await browser.newPage();
30
31await page.goto('https://developers.google.com/web/');
32
33// Type into search box.
34await page.type('#searchbox input', 'Headless Chrome');
35
36// Wait for suggest overlay to appear and click "show all results".
37const allResultsSelector = '.devsite-suggest-all-results';
38await page.waitForSelector(allResultsSelector);
39await page.click(allResultsSelector);
40
41// Wait for the results page to load and display the results.
42const resultsSelector = '.gsc-results .gsc-thumbnail-inside a.gs-title';
43await page.waitForSelector(resultsSelector);
44
45// Extract the results from the page.
46const links = await page.evaluate(resultsSelector => {
47 const anchors = Array.from(document.querySelectorAll(resultsSelector));
48 return anchors.map(anchor => {
49 const title = anchor.textContent.split('|')[0].trim();
50 return `${title} - ${anchor.href}`;
51 });
52}, resultsSelector);
53console.log(links.join('\n'));
54
55await browser.close();
56
57})();