-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.js
48 lines (36 loc) · 1.21 KB
/
scrape.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const puppeteer = require("puppeteer");
const axios = require("axios");
const fs = require("fs");
const path = require("path");
(async () => {
const url = "https://www.example.com/your-target-webpage";
// Launch a headless browser
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Navigate to the URL
await page.goto(url, { waitUntil: "networkidle2" });
// Get the MP3 link from the audio element
const mp3Link = await page.$eval("audio", (audio) => audio.src);
// Log the MP3 link
console.log("MP3 Link: ", mp3Link);
// Download the MP3 file
const response = await axios({
method: "GET",
url: mp3Link,
responseType: "stream",
});
// Extract the file name from the URL
const urlPath = new URL(mp3Link).pathname;
const filename = path.basename(urlPath);
// Save the MP3 file to the local filesystem
const writer = fs.createWriteStream(filename);
response.data.pipe(writer);
writer.on("finish", () => {
console.log(`File has been downloaded and saved as ${filename}.`);
});
writer.on("error", (error) => {
console.error("Error while downloading and saving the file: ", error);
});
// Close the browser
await browser.close();
})();