Javascript 未处理的PromisejectionWarning:TypeError:undefined不是函数

Javascript 未处理的PromisejectionWarning:TypeError:undefined不是函数,javascript,node.js,Javascript,Node.js,我一直在努力让这个刮刀工作,按照教程从 我曾经尝试过删除亚马逊,以防黄页不起作用。我从节点12到节点13。我尝试使用完整的xpath。如果保存时的格式导致错误,则禁用prettier 我不明白为什么这个代码不起作用。我向上帝发誓我从youtube上逐行复制 (node:1740) UnhandledPromiseRejectionWarning: TypeError: undefined is not a function at scrapeProduct (C:\Users\hell

我一直在努力让这个刮刀工作,按照教程从

我曾经尝试过删除亚马逊,以防黄页不起作用。我从节点12到节点13。我尝试使用完整的xpath。如果保存时的格式导致错误,则禁用prettier

我不明白为什么这个代码不起作用。我向上帝发誓我从youtube上逐行复制

(node:1740) UnhandledPromiseRejectionWarning: TypeError: undefined is not a function
    at scrapeProduct (C:\Users\hello\coding\scrapy\server.js:8:16)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:1740) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1740) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

async/await
中始终使用try/catch,每当wait等待的承诺被拒绝时,它将抛出一个应被catch捕获的错误

const puppeteer = require("puppeteer");

async function scrapeProduct(url) {
    try {
        const browser = await puppeteer.launch();
        const page = await browser.newPage();
        await page.goto(url);

        const [el] = page.$x('/html/body/div[2]/div/div[1]/div[1]/div[2]/div[2]/div[1]/div/div/div[2]/h2/a/span');
        const src = await el.getProperty("src");
        const srcTxt = await src.jsonValue();


        console.log({ srcTxt });
    } catch (e) {
        console.log(e, "ERROR");
    }
}

scrapeProduct('https://www.yellowpages.com/search?search_terms=cpa&geo_location_terms=New%20York%2C%20NY&page=1');

在您的环境中运行上述代码,您将了解问题的原因。

您在页面前面遗漏了等待。$X谢谢,我不敢相信。。。那是两个小时的工作???
const puppeteer = require("puppeteer");

async function scrapeProduct(url) {
    try {
        const browser = await puppeteer.launch();
        const page = await browser.newPage();
        await page.goto(url);

        const [el] = page.$x('/html/body/div[2]/div/div[1]/div[1]/div[2]/div[2]/div[1]/div/div/div[2]/h2/a/span');
        const src = await el.getProperty("src");
        const srcTxt = await src.jsonValue();


        console.log({ srcTxt });
    } catch (e) {
        console.log(e, "ERROR");
    }
}

scrapeProduct('https://www.yellowpages.com/search?search_terms=cpa&geo_location_terms=New%20York%2C%20NY&page=1');