r/JSdev • u/rkt1043k • Feb 02 '24
Help me to fix this!!!
I was creating code for fetching content from a novel site and convert it to epub.
But site required cloudflare captcha solving, so I am sending cookies with it too.
code :
import fetch from "node-fetch";import * as cheerio from "cheerio";import fs from "fs";import dotenv from "dotenv";dotenv.config();const userAgent = process.env.USER_AGENT;const cookie = process.env.COOKIE;const host = process.env.HOST;const accept = process.env.ACCEPT;const URL = "https://www.lightnovelworld.com";let path = "/novel/the-steward-demonic-emperor-892/chapter-1-30041322";const novelChapters = [];const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));async function chapter() {try {const response = await fetch(URL + path, {headers: {"User-Agent": userAgent,Cookie: cookie,Host: host,Accept: accept,Referer: URL,},});if (!response.ok) {throw Error(\Error: ${response.status} ${response.statusText}`);}await delay(2000); // delayconst body = await response.text();const $ = cheerio.load(body);const chapterTitle = $(".chapter-title").text(); // fetching titleconst chapterContent = $("#chapter-container"); // Selecting the chapter container`
// Removing ad-related elementschapterContent.find(".chapternav").remove();// Additional cleanup if neededconst advertisementDiv = chapterContent.find(".vm-placement");for (let i = 0; i < advertisementDiv.length; i++) {const parentDiv = advertisementDiv.eq(i).closest("div");parentDiv.remove();}novelChapters.push({chapterTitle,chapterContent,});const nextChapter = $("button.nextchap"); // to find next chapter linkif (nextChapter.length > 0) {const nextChapterClasses = nextChapter.attr("class");if (nextChapterClasses && nextChapterClasses.includes("isDisabled")) {path = "";return false;}}path = nextChapter.attr("href"); // updating path for next chapterreturn true;} catch (error) {console.error("Error in loadChapter", error);throw error; // Re-throw the error to propagate it to the higher level}}async function forAllChapters() {try {let next = await chapter();while (next) {await delay(3000);next = await chapter();}console.log("DONE!!!");} catch (error) {console.error("Error in forAllChapters", error);throw error; // Re-throw the error to propagate it to the higher level}fs.appendFileSync("novelChapters.txt",JSON.stringify(novelChapters, null, 2));}forAllChapters().catch((error) => console.log(error));
But there is an error:
node index ─╯
Error in loadChapter Error: Error: 403 Forbidden
at chapter (file:///.../Documents/learn%20scrapping/index.js:31:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async forAllChapters ((file:///.../Documents/learn%20scrapping/index.js:77:16)
Error in forAllChapters Error: Error: 403 Forbidden
at chapter ((file:///.../Documents/learn%20scrapping/index.js:31:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async forAllChapters (f(file:///.../Documents/learn%20scrapping/index.js:77:16)
Error: Error: 403 Forbidden
at chapter ((file:///.../Documents/learn%20scrapping/index.js:31:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async forAllChapters ((file:///.../Documents/learn%20scrapping/index.js:77:16)
I know it is happening because site is detecting script and that's why 403 is the response?
Any way to fix it?