最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Puppeteer page.content() - writing resolved promise into list - Stack Overflow

programmeradmin1浏览0评论

I am trying to write a page's content into a list with puppeteer.

My problem is too basic for other solutions out there. The reason I am failing is because I am too ignorant to get the resolved promise from an asynchronous function. I looked at the js documentation, I looked at similar answers, to no avail.

I also tried multiple ways to resolve the page.content() promise within the asynchronous function, still failing miserably.

async function getPageContent(website) {
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();

  await page.goto(website, { waitUntil: "networkidle2" });
  const websiteContent = await page.content();
  await browser.close();

  return websiteContent;
}

var htmlList = [];
var ws = "/";
var googleHtml = getPageContent(ws);

htmlList.push(googleHtml);

console.log(htmlList);

The result is [Promise { pending }], which I understand is due to the promise status being unresolved. What I would like is the actual string of google's DOM.

Thank you for any help in this specific promise / async / puppeteer crash course.

I am trying to write a page's content into a list with puppeteer.

My problem is too basic for other solutions out there. The reason I am failing is because I am too ignorant to get the resolved promise from an asynchronous function. I looked at the js documentation, I looked at similar answers, to no avail.

I also tried multiple ways to resolve the page.content() promise within the asynchronous function, still failing miserably.

async function getPageContent(website) {
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();

  await page.goto(website, { waitUntil: "networkidle2" });
  const websiteContent = await page.content();
  await browser.close();

  return websiteContent;
}

var htmlList = [];
var ws = "https://www.google./";
var googleHtml = getPageContent(ws);

htmlList.push(googleHtml);

console.log(htmlList);

The result is [Promise { pending }], which I understand is due to the promise status being unresolved. What I would like is the actual string of google's DOM.

Thank you for any help in this specific promise / async / puppeteer crash course.

Share Improve this question edited Aug 13, 2019 at 15:51 hardkoded 21.8k3 gold badges61 silver badges74 bronze badges asked Aug 13, 2019 at 15:02 GregKGregK 5936 silver badges16 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 4

You have to await getPageContent:

var htmlList = [];
var ws = "https://www.google./";
var googleHtml = await getPageContent(ws);
发布评论

评论列表(0)

  1. 暂无评论