Last active
September 27, 2018 06:47
-
-
Save zdying/e68166947243e993e9a50d6e49cc3a92 to your computer and use it in GitHub Desktop.
Puppeteer amazon list example.
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
// 引入puppeteer模块 | |
const puppeteer = require('puppeteer'); | |
// puppeteer内置的设备描述信息 | |
const devices = require('puppeteer/DeviceDescriptors'); | |
// 模拟iPhone 6 Plus | |
const iPhone6P = devices['iPhone 6 Plus']; | |
async function run() { | |
console.log('正在获取数据,请查看打开的浏览器窗口...'); | |
// 启动浏览器并设置窗口大小 | |
const browser = await puppeteer.launch({ | |
headless: false, | |
args: [`--window-size=414,800`] | |
}); | |
// 打开新页面 | |
const page = await browser.newPage(); | |
// 模拟移动设备 | |
await page.emulate(iPhone6P); | |
// 输入网址并加载渲染 | |
await page.goto('http://z.cn'); | |
// 等待1s | |
await page.waitFor(1000); | |
// 如果页面有浮层,关闭浮层然后继续 | |
let $closeBtn = await page.$('#cnmb-gw-int-signup-promptclose'); | |
if ($closeBtn) { | |
await $closeBtn.click(); | |
} | |
// 输入关键字`JavaScript` | |
await page.type( | |
'#nav-search-keywords', | |
'JavaScript', | |
{ delay: 100 } | |
); | |
// 点击搜索按钮 | |
await page.click('.nav-search-submit .nav-input'); | |
// 等待新页面加载完成 | |
await page.waitForNavigation(); | |
// 获取搜索结果列表 | |
const items = await page.$$eval('#resultItems li a', els => { | |
return els.map(el => { | |
// 标题 | |
let $title = el.querySelector('.sx-title'); | |
// 价格 | |
let $price = el.querySelector('.a-color-price'); | |
// 作者 | |
let $author = el.querySelector('.sx-title + div'); | |
// 缩略图 | |
let $img = el.querySelector('.sx-product-image'); | |
return { | |
title: $title.textContent.trim(), | |
price: $price.textContent.trim(), | |
author: $author.textContent.trim(), | |
img: $img.href | |
} | |
}); | |
}); | |
// 打印结果信息 | |
console.log('获取到的列表数据:'); | |
console.log(JSON.stringify(items, null, 2)); | |
// 在页面中添加提示浮层 | |
await page.evaluate(() => { | |
let html = '<p style="background: #d62609cf;position: fixed;width: 100%;border: 1px solid #c55;padding: 6px;color: white;">获取完毕,请查看Node.js控制台。</p>'; | |
document.body.innerHTML += html; | |
}); | |
// 等待2秒后关闭浏览器 | |
await page.waitFor(2000); | |
await browser.close(); | |
} | |
run().catch(err => { | |
console.log(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment