简体   繁体   English

如何在 CPanel 上使用 puppeteer

[英]How to use puppeteer on CPanel

I have a puppeteer script that works on my local PC.我有一个可以在本地 PC 上运行的木偶脚本。 I'd like to host the script on my CPanel so that I can run it every day through my namecheap hosting service.我想在我的 CPanel 上托管脚本,这样我就可以每天通过我的 namecheap 托管服务运行它。 I installed a nodejs app on my CPanel, and I try to run my code but I get this error.我在我的 CPanel 上安装了一个 nodejs 应用程序,我尝试运行我的代码,但出现此错误。 I am trying to scrape some numbers from the inte.net and then upload them into mySQL. Keep in mind I have no idea what I'm actually doing.我正在尝试从 inte.net 中抓取一些数字,然后将它们上传到 mySQL。请记住,我不知道我实际上在做什么。

gaspricesapi@1.0.0 start /home/sameryalda/GasPricesAPI
node app.js
stderr:
npm WARN lifecycle The node binary used for scripts is 
/home/sameryalda/nodevenv/GasPricesAPI/10/bin/node but npm is using 
/opt/alt/alt- 
nodejs10/root/usr/bin/node itself. Use the --scripts-prepend-node- 
path option to include 
the path for the node binary npm was executed with.
/home/sameryalda/GasPricesAPI/app.js:1
import puppeteer from 'puppeteer';
       ^^^^^^^^^
SyntaxError: Unexpected identifier
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js 
(internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain 
(internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! gaspricesapi@1.0.0 start: node app.js
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the gaspricesapi@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely 
additional logging 
output above.`

nodejs app setup: nodejs 应用程序设置:

在此处输入图像描述

import puppeteer from 'puppeteer';
import fetch from 'node-fetch';
import mysql from 'mysql';

let con = mysql.createConnection({
    host: "localhost",
    user: "test",
    password: "test"
});

let scrapeAvgGas = async (url) => {
    let average = 0;
    let numRecords = 0;
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(url);
    for(let i = 0; (await page.$x(`//[@id="spnGB27562Price${i}"]`)) != ''; i++) {
        const [el] = await page.$x(`//[@id="spnGB27562Price${i}"]`);
        const txt = await el.getProperty('textContent');
        let rawTxt = await txt.jsonValue();
        rawTxt = Number(rawTxt.replace(/[^\d.-]/g, ''));
        average += rawTxt;
        numRecords++;
    }
    return Math.round(((average / numRecords) + Number.EPSILON) * 100) / 100
}

let scrapeBridgeGas = async (url, xpath) => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(url);
    const [el] = await page.$x(xpath);
    const txt = await el.getProperty('textContent');
    let rawTxt = await txt.jsonValue();
    rawTxt = Number(rawTxt.replace(/[^\d.-]/g, ''));
    return rawTxt;
}

async function getExchange(url) {
    try {
        const response = await fetch(url, {
            method: 'GET',
            credentials: 'same-origin'
        });
        const exch = await response.json();
        return exch.observations[0].FXUSDCAD.v;
    } catch (error) {
        console.error(error);
    }
}

let gasWindsor = await scrapeAvgGas();
let gasBridge = await 
scrapeBridgeGas('https://www.ambassadorbridge.com/fuel-price- 
list/', '//*[@id="qodef-page-content"]/div/div/p/text()[4]')
let premBridge = await 
scrapeBridgeGas('https://www.ambassadorbridge.com/fuel-price- 
list/', '//*[@id="qodef-page-content"]/div/div/p/text()[5]')
let exchRate = await 
getExchange('https://www.bankofcanada.ca/valet/observations/FXUSDCAD/json?recent=1')

con.connect(function(err) {
    if (err) throw err;
    console.log("Connected!");
    let sql = "INSERT INTO customers (name, price) VALUES ?";
    let values = [
        ['gasWindsor', gasWindsor],
        ['gasBridge', gasBridge],
        ['premBridge', premBridge],
        ['exchRate', exchRate],
    ];
    con.query(sql, [values], function (err, result) {
        if (err) throw err;
        console.log("Number of records inserted: " + result.affectedRows);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM