我创建了一个网络抓取应用程序,用于检查电子商务网站上的特定问题。
它的作用:
- 循环访问页面数组
- 检查每个页面上的条件
- 如果满足条件 - 将页面推送到临时数组
- 发送一封以临时数组作为正文的电子邮件
我将该函数包装在 cronjob 函数中。 在我的本地计算机上它运行良好。
这样部署:
- headless :正确
- '--无沙箱',
- '--disable-setuid-sandbox'
- 将 pptr buildpack 链接添加到 heroku 中的设置
- slugsize 为 500 MiB 中的 259.6 MiB
没有成功。
- 将启动超时设置为 120 秒(而不是 60 秒)
它成功了。但只跑了一次。
由于它想要每天多次运行该函数,因此我需要解决该问题。
我正在运行另一个应用程序,它使用相同的 cronjob 和通知功能,并且它可以在 heroku 上运行。
如果有人感兴趣,这是我的代码。
const puppeteer = require('puppeteer');
const nodemailer = require("nodemailer");
const CronJob = require('cron').CronJob;
let articleInfo ='';
const mailArr = [];
let body = '';
const testArr = [
'https://bxxxx..', https://b.xxx..', https://b.xxxx..',
];
async function sendNotification() {
let transporter = nodemailer.createTransport({
host: 'mail.brxxxxx.dxx',
port: 587,
secure: false,
auth: {
user: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b737e625b793563636363" rel="noreferrer noopener nofollow">[email protected]</a>',
pass: process.env.heyBfPW2
}
});
let textToSend = 'This is the heading';
let htmlText = body;
let info = await transporter.sendMail({
from: '"BB Checker" <hey@baxxxxx>',
to: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="601318184e4e18184e4e4e181820070d01090c4e030f0d" rel="noreferrer noopener nofollow">[email protected]</a>",
subject: 'Hi there',
text: textToSend,
html: htmlText
});
console.log("Message sent: %s", info.messageId);
}
async function boxLookUp (item) {
const browser = await puppeteer.launch({
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
],
});
const page = await browser.newPage();
await page.goto(item);
const content = await page.$eval('.set-article-info', div => div.textContent);
const title = await page.$eval('.product--title', div => div.textContent);
const orderNumber = await page.$eval('.entry--content', div => div.textContent);
// Check if deliveryTime is already updated
try {
await page.waitForSelector('.delivery--text-more-is-coming');
// if not
} catch (e) {
if (e instanceof puppeteer.errors.TimeoutError) {
// if not updated check if all parts of set are available
if (content != '3 von 3 Artikeln ausgewählt' && content != '4 von 4 Artikeln ausgewählt' && content != '5 von 5 Artikeln ausgewählt'){
articleInfo = `${title} ${orderNumber} ${item}`;
mailArr.push(articleInfo)
}
}
}
await browser.close();
};
const checkBoxes = async (arr) => {
for (const i of arr) {
await boxLookUp(i);
}
console.log(mailArr)
body = mailArr.toString();
sendNotification();
}
async function startCron() {
let job = new CronJob('0 */10 8-23 * * *', function() { // run every_10_minutes_between_8_and_11
checkBoxes(testArr);
}, null, true, null, null, true);
job.start();
}
startCron();
最佳答案
同样的问题已经持续三天了。这里可能有帮助:https://stackoverflow.com/a/55861535/13735374
必须与 Procfile 一起完成。
关于node.js - Heroku 错误 R10(启动超时) Node 上的 Puppeteer(网页抓取应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67498169/