I’m scraping book titles and URLs from a webpage using Playwright, and the data logs correctly in the console. However, I can’t figure out how to write this scraped content into a Markdown (.md) file.
Here’s the script:
const { chromium } = require('playwright');
const fs = require('fs');
(async () => {
const browser = await chromium.launch({ headless: true });
const page = await browser.newPage();
await page.goto("https://books.toscrape.com/");
const listcontent = await page.evaluate(() => {
const data = [];
document.querySelectorAll(".product_pod").forEach((book) => {
const title = book.querySelector('.thumbnail').getAttribute("alt");
const url = book.querySelector('a').getAttribute("href");
data.push({ title, url });
});
return data;
});
// Working console output
for (const { title, url } of listcontent) {
console.log(`[${title}](${url})`);
}
// ❌ Problem: I can’t get this to write properly into a Markdown file
fs.promises.writeFile(`file.md`, `---\n---\n`);
await browser.close();
})();
The console shows the correct data, but the .md file remains empty or doesn’t include the list.
How can I properly write the scraped data into a Markdown file using Playwright and Node.js?