我一直在尝试按照以下教程操作此刮板
https://www.youtube.com/watch?v=TzZ3YOUhCxo
我曾尝试刮刮亚马逊以防黄页无法正常工作。我从节点12转到节点13。我尝试使用完整的xpath。如果保存时的格式导致错误,请禁用更漂亮。
帮助我不明白为什么此代码无法正常工作。我向上帝发誓,我要逐行从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.
const puppeteer = require("puppeteer");
async function scrapeProduct(url) {
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});
}
scrapeProduct('https://www.yellowpages.com/search?search_terms=cpa&geo_location_terms=New%20York%2C%20NY&page=1');
最佳答案
每当在一个等待中被拒绝的承诺被拒绝时,总是在async/await
中使用try / catch,它将抛出一个错误,应该由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');
在您的环境中运行上面的代码,您将了解导致问题的原因。
关于javascript - UnhandledPromiseRejectionWarning:TypeError:未定义不是函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60333432/