const puppeteer = require ('puppeteer');
async function scrapeA (url) {
return new Promise (async (resolve, reject) => {
try{
**const browser = await puppeteer.launch();**
const page = await browser.newPage();
await page.goto(url)
page.$eval('#city', elem => elem.click());
const [el] = await page.$x('//*[@id="content"]/div[1]/div[2]/h1/a');
const txt1 = await el.getProperty('innerText');
const nameA = await txt1.jsonValue();
const [el2] = await page.$x('//*[@id="offerPrice_328504"]');
const txt2 = await el2.getProperty('innerText');
const priceA = await txt2.jsonValue();
const [el3] = await page.$x('//*[@id="inventoryTableBody"]/div[5]/div[3]/span[1]');
const txt3 = await el3.getProperty('innerText');
const quantA = await txt3.jsonValue();
const [el4] = await page.$x('//*//*[@id="inventoryTableBody"]/div[5]/div[2]/text()[1]');
const txt4 = await el4.getProperty('textContent');
const locA = await txt4.jsonValue();
console.log({nameA},{priceA},{quantA},{locA});
browser.close();
return resolve();
} catch (e) {
return reject(e);
}
})
}
scrapeA(webpageurl);
此代码可在我的代码编辑器中使用。我试图对此有所了解,所以将其移至GoormIDE中。浏览器进程无法启动
我尝试了2个不同软件包的所有组合:puppeteer和puppeteer-core
和3种不同的启动设置:
const browser = await puppeteer.launch();
const browser = await puppeteer.launch({headless: false});
const browser = await puppeteer.launch({ignoreDefaultArgs: ['--disable-extensions']});
答案 0 :(得分:0)
使用安装缺少的软件包
apt-get install gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget libgbm-dev
尝试一下
const browser = await puppeteer.launch({ args: ['--no-sandbox'] });