用木偶操纵亚马逊
Scraping amazon with puppeteer
我目前正在做一些个人项目,我只是想做一些亚马逊的抓取,这样我就可以获得产品的详细信息,比如名称和价格。
我发现对产品名称和价格使用相同 ID 的最一致的视图是移动视图,所以这就是我使用它的原因。
问题是我拿不到价格
我对价格中的名称(有效)进行了完全相同的查询选择器,但没有成功。
const puppeteer = require('puppeteer');
const url = 'https://www.amazon.com/dp/B01MUAGZ49';
(async () => {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.setViewport({ width: 360, height: 640 });
await page.goto(url);
let producData = await page.evaluate(() => {
let productDetails = [];
let elements = document.querySelectorAll('#a-page');
elements.forEach(element => {
let detailsJson = {};
try {
detailsJson.name = element.querySelector('h1#title').innerText;
detailsJson.price = element.querySelector('#newBuyBoxPrice').innerText;
} catch (exception) {}
productDetails.push(detailsJson);
});
return productDetails;
});
console.dir(producData);
})();
我应该在 console.dir 中得到名称和价格,但现在我只得到
[ { name: 'Nintendo Switch – Neon Red and Neon Blue Joy-Con ' } ]
仅设置视口高度和重量不足以完全模拟移动浏览器。现在该页面假定您只有一个非常小的浏览器 window.
模拟移动设备最简单的方法是使用函数page.emulate
and the default DeviceDesriptors,其中包含有关大量移动设备的信息。
引自 page.emulate
的文档:
Emulates given device metrics and user agent. This method is a shortcut for calling two methods:
To aid emulation, puppeteer provides a list of device descriptors which can be obtained via the require('puppeteer/DeviceDescriptors')
command. [...]
例子
这是一个关于如何在访问页面时模拟 iPhone 的示例。
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const iPhone = devices['iPhone 6'];
const url = '...';
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.emulate(iPhone);
await page.goto(url);
// Simlified page.evaluate
let producData = await page.evaluate(() => ({
name: document.querySelector('#a-page h1#title').innerText,
price: document.querySelector('#a-page #newBuyBoxPrice').innerText
}));
console.dir(producData);
})();
我也稍微简化了你的page.evaluate
,但你当然也可以在page.goto
之后使用你的原始代码。这为我返回了产品的名称和价格。
我目前正在做一些个人项目,我只是想做一些亚马逊的抓取,这样我就可以获得产品的详细信息,比如名称和价格。
我发现对产品名称和价格使用相同 ID 的最一致的视图是移动视图,所以这就是我使用它的原因。
问题是我拿不到价格
我对价格中的名称(有效)进行了完全相同的查询选择器,但没有成功。
const puppeteer = require('puppeteer');
const url = 'https://www.amazon.com/dp/B01MUAGZ49';
(async () => {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.setViewport({ width: 360, height: 640 });
await page.goto(url);
let producData = await page.evaluate(() => {
let productDetails = [];
let elements = document.querySelectorAll('#a-page');
elements.forEach(element => {
let detailsJson = {};
try {
detailsJson.name = element.querySelector('h1#title').innerText;
detailsJson.price = element.querySelector('#newBuyBoxPrice').innerText;
} catch (exception) {}
productDetails.push(detailsJson);
});
return productDetails;
});
console.dir(producData);
})();
我应该在 console.dir 中得到名称和价格,但现在我只得到
[ { name: 'Nintendo Switch – Neon Red and Neon Blue Joy-Con ' } ]
仅设置视口高度和重量不足以完全模拟移动浏览器。现在该页面假定您只有一个非常小的浏览器 window.
模拟移动设备最简单的方法是使用函数page.emulate
and the default DeviceDesriptors,其中包含有关大量移动设备的信息。
引自 page.emulate
的文档:
Emulates given device metrics and user agent. This method is a shortcut for calling two methods:
To aid emulation, puppeteer provides a list of device descriptors which can be obtained via the
require('puppeteer/DeviceDescriptors')
command. [...]
例子
这是一个关于如何在访问页面时模拟 iPhone 的示例。
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const iPhone = devices['iPhone 6'];
const url = '...';
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.emulate(iPhone);
await page.goto(url);
// Simlified page.evaluate
let producData = await page.evaluate(() => ({
name: document.querySelector('#a-page h1#title').innerText,
price: document.querySelector('#a-page #newBuyBoxPrice').innerText
}));
console.dir(producData);
})();
我也稍微简化了你的page.evaluate
,但你当然也可以在page.goto
之后使用你的原始代码。这为我返回了产品的名称和价格。