{"id":57151,"date":"2024-08-16T17:04:29","date_gmt":"2024-08-16T16:04:29","guid":{"rendered":"https:\/\/proxidize.com\/?post_type=use-cases&#038;p=57151"},"modified":"2025-10-02T12:26:16","modified_gmt":"2025-10-02T11:26:16","slug":"web-scraping-with-javascript","status":"publish","type":"blog","link":"https:\/\/proxidize.com\/blog\/web-scraping-with-javascript\/","title":{"rendered":"Beginner&#8217;s Guide To Web Scraping with JavaScript"},"content":{"rendered":"\n<p>Previously, we discussed the basics of web scraping as well as breaking down how to scrape using Python, BeautifulSoup, pyQuery, and introduced some powerful <a href=\"https:\/\/proxidize.com\/blog\/web-scraping-tools\/\">tools for scraping<\/a>. This article will cover one of the most popular programming languages: JavaScript.&nbsp;<\/p>\n\n\n\n<p>Throughout this guide, we will cover how web scraping with JavaScript compares to other programming languages as a tool for scraping, provide a step-by-step process on how to set up and start web scraping with JavaScript including data handling and storage methods, and some ways to enhance your scraping projects.&nbsp;<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Main JavaScript Libraries<\/h2>\n\n\n\n<p><a href=\"https:\/\/proxidize.com\/blog\/what-is-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript<\/a> is a powerful programming language that is mostly used for dynamic websites . Includes many libraries that help it perform the tasks it needs to do. For the purposes of learning about web scraping with JavaScript, we will explore three libraries that assist with scraping, those being Cheerio, Puppeteer, and Axios.<\/p>\n\n\n\n<p>Cheerio allows developers to parse and manipulate HTML and XLM documents. This allows a user to find the exact website URLs they need to scrape to gather the information they need. <a href=\"https:\/\/cheerio.js.org\/\" data-type=\"link\" data-id=\"https:\/\/cheerio.js.org\/\" target=\"_blank\" rel=\"noopener\">Cheerio<\/a> is fast and supports CSS selectors, making it ideal for web scraping. Puppeteer provides high-level API to control headless browsers such as Chrome. It is used for scraping dynamic content, automating form submissions, and taking screenshots. It supports JavaScript, CSS3, and HTML5. <a href=\"https:\/\/axios-http.com\/docs\/intro\" data-type=\"link\" data-id=\"https:\/\/axios-http.com\/docs\/intro\" target=\"_blank\" rel=\"noopener\">Axois<\/a> allows users to make requests to websites and servers. Instead of presenting the results visually, it presents them through code, allowing users to manipulate the response easier.&nbsp;<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized centered\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/comparison-with-other-coding-languages-1024x576.png\" alt=\"The logos of JavaScript, Python, php, and ruby titled &quot;Comparison with Other Coding Languages&quot;.\" class=\"wp-image-57152\" style=\"object-fit:cover\" srcset=\"https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/comparison-with-other-coding-languages-1024x576.png 1024w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/comparison-with-other-coding-languages-300x169.png 300w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/comparison-with-other-coding-languages-768x432.png 768w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/comparison-with-other-coding-languages-1536x864.png 1536w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/comparison-with-other-coding-languages-600x338.png 600w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/comparison-with-other-coding-languages.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Comparison with Other Coding Languages&nbsp;<\/h2>\n\n\n\n<p>While JavaScript is a powerful web scraping language, it is not the only language capable of handling scraping projects. This section will compare Python, Ruby, and PHP and balance out their strengths and weaknesses against JavaScript.&nbsp;<\/p>\n\n\n\n<p>Python is a simple and easy to read language and is excellent for beginners just starting off with scraping. It offers popular libraries such as BeautifulSoup (we have written an article detailing how to perform <a href=\"https:\/\/proxidize.com\/use-cases\/web-scraping-with-beautiful-soup\/\">Web Scraping with Beautiful Soup<\/a>). It has powerful data analysis and a large community support to assist anyone with any problems or issues they encounter. However, it is slower than JavaScript and requires some additional tools when handling JS-heavy websites.&nbsp;<\/p>\n\n\n\n<p>Ruby offers elegant and concise syntax and offers its own personal libraries such as Nokogiri and Watir which makes web scraping more straightforward. When compared to JavaScript, it does tend to run slower and is a less popular scraping language than Python or JS.&nbsp;<\/p>\n\n\n\n<p>PHP is simple and offers strong support for server-side scraping. It similarly offers scraping-specific libraries such as cURL and Simple HTML DOM. One of its downsides is that it is less efficient for client-side scripting and is not as robust for handling multiple tasks.&nbsp;<\/p>\n\n\n\n<p>Now that we have covered a basic understanding of JavaScript and how it compares to other programming languages, it is time to start writing a script for web scraping with JavaScript. The next section will break down the script into parts that will allow you to understand why each line of code needs to be written and what function it possesses.<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized centered\"><img decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/setting-up-and-using-web-scraping-with-javascript-1024x576.png\" alt=\"A diagram with the title &quot;Setting Up and Using Web Scraping with JavaScript&quot;.\" class=\"wp-image-57156\" style=\"object-fit:cover\" srcset=\"https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/setting-up-and-using-web-scraping-with-javascript-1024x576.png 1024w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/setting-up-and-using-web-scraping-with-javascript-300x169.png 300w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/setting-up-and-using-web-scraping-with-javascript-768x432.png 768w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/setting-up-and-using-web-scraping-with-javascript-1536x864.png 1536w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/setting-up-and-using-web-scraping-with-javascript-600x338.png 600w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/setting-up-and-using-web-scraping-with-javascript.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Setting Up and Using Web Scraping with JavaScript<\/h2>\n\n\n\n<p>There are a few preliminary tasks you must perform before you begin web scraping. For starters, you would need to set up your coding environment and set up a project directory that will organize your code and dependencies, and you would need to start a Node.js project so you can begin writing in JavaScript.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Setting Up the Environment<\/h3>\n\n\n\n<p>Step 1: Install Node.js&nbsp;<\/p>\n\n\n\n<p>The very first step you would need to follow is installing a JavaScript runtime environment onto your device. This will allow you to perform web scraping tasks. It can be done by visiting the <a href=\"https:\/\/nodejs.org\/en\" target=\"_blank\" rel=\"noopener\">JavaScript website<\/a> and downloading the latest version that is appropriate to your device specifications. After you have downloaded the file, open your terminal or command prompt and type in \u201cnode -v\u201d to ensure it has been installed correctly and is ready to go.&nbsp;<\/p>\n\n\n\n<p>Step 2: Set Up a Project Directory<\/p>\n\n\n\n<p>Next up, you would need to create a new directory. This will help organize your code and dependencies. You can do this by typing in the following prompt:<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e0def4;--cbp-line-number-width:calc(1 * 0.6 * .75rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#232136;display:none;background-color:#e0def4\" aria-label=\"Copy\" data-copied-text=\"Copied!\" data-has-text-button=\"textSimple\" data-inside-header-type=\"none\" aria-live=\"polite\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>mkdir web-scraping-js\ncd web-scraping-js<\/textarea><\/pre><span class=\"cbp-btn-text\">Copy<\/span><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #E0DEF4; font-style: italic\">mkdir<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">web<\/span><span style=\"color: #3E8FB0\">-<\/span><span style=\"color: #E0DEF4; font-style: italic\">scraping<\/span><span style=\"color: #3E8FB0\">-<\/span><span style=\"color: #E0DEF4; font-style: italic\">js<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4; font-style: italic\">cd<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">web<\/span><span style=\"color: #3E8FB0\">-<\/span><span style=\"color: #E0DEF4; font-style: italic\">scraping<\/span><span style=\"color: #3E8FB0\">-<\/span><span style=\"color: #E0DEF4; font-style: italic\">js<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Step 3: Initialize a Node.js Project:<\/p>\n\n\n\n<p>The last step of the initial setup involves creating a package.json file to manage your project on. This is done by writing the following prompt into your terminal or command prompt.&nbsp;<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e0def4;--cbp-line-number-width:calc(1 * 0.6 * .75rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#232136;display:none;background-color:#e0def4\" aria-label=\"Copy\" data-copied-text=\"Copied!\" data-has-text-button=\"textSimple\" data-inside-header-type=\"none\" aria-live=\"polite\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>npm init -y<\/textarea><\/pre><span class=\"cbp-btn-text\">Copy<\/span><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #E0DEF4; font-style: italic\">npm<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">init<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">-<\/span><span style=\"color: #E0DEF4; font-style: italic\">y<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Writing a Web Scraping Script&nbsp;<\/h3>\n\n\n\n<p>Now that you have your set up taken care of, it is time to start web scraping with JavaScript. For the purposes of this article, we will be providing an example of scraping using the libraries Axios and Cheerio:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Scraping Static Web Pages<\/h4>\n\n\n\n<p>1. Fetch HTML Content:<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e0def4;--cbp-line-number-width:calc(2 * 0.6 * .75rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#232136;display:none;background-color:#e0def4\" aria-label=\"Copy\" data-copied-text=\"Copied!\" data-has-text-button=\"textSimple\" data-inside-header-type=\"none\" aria-live=\"polite\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>const axios = require('axios');\n\nasync function fetchHTML(url) {\n  try {\n    const { data } = await axios.get(url);\n    return data;\n  } catch (error) {\n    console.error(`Error fetching HTML: ${error}`);\n    throw error; \/\/ Rethrow the error to ensure calling functions can handle it\n  }\n}\n\nmodule.exports = fetchHTML; \/\/ If you want to use this function in other files <\/textarea><\/pre><span class=\"cbp-btn-text\">Copy<\/span><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">axios<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">require<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;axios&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">async<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">fetchHTML<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #C4A7E7; font-style: italic\">url<\/span><span style=\"color: #908CAA\">)<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">try<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">await<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">axios<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">get<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #E0DEF4; font-style: italic\">url<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">catch<\/span><span style=\"color: #E0DEF4\"> (<\/span><span style=\"color: #E0DEF4; font-style: italic\">error<\/span><span style=\"color: #E0DEF4\">) <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #E0DEF4; font-style: italic\">console<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">error<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">`Error fetching HTML: <\/span><span style=\"color: #908CAA\">${<\/span><span style=\"color: #E0DEF4; font-style: italic\">error<\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #F6C177\">`<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">throw<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">error<\/span><span style=\"color: #908CAA\">;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA; font-style: italic\">\/\/<\/span><span style=\"color: #6E6A86; font-style: italic\"> Rethrow the error to ensure calling functions can handle it<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #9CCFD8\">module<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #9CCFD8\">exports<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">fetchHTML<\/span><span style=\"color: #908CAA\">;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA; font-style: italic\">\/\/<\/span><span style=\"color: #6E6A86; font-style: italic\"> If you want to use this function in other files <\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>2. Parse HTML Content:<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e0def4;--cbp-line-number-width:calc(2 * 0.6 * .75rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#232136;display:none;background-color:#e0def4\" aria-label=\"Copy\" data-copied-text=\"Copied!\" data-has-text-button=\"textSimple\" data-inside-header-type=\"none\" aria-live=\"polite\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>const axios = require('axios');\nconst cheerio = require('cheerio');\n\n\/\/ Function to fetch HTML\nasync function fetchHTML(url) {\n  try {\n    const { data } = await axios.get(url);\n    return data;\n  } catch (error) {\n    console.error(`Error fetching HTML: ${error}`);\n    throw error; \/\/ Rethrow the error to ensure calling functions can handle it\n  }\n}\n\n\/\/ Function to scrape data\nasync function scrapeData(url) {\n  try {\n    const html = await fetchHTML(url);\n    const $ = cheerio.load(html);\n\n    \/\/ Example: Extracting titles from a blog\n    const titles = [];\n    $('h2.post-title').each((index, element) => {\n      titles.push($(element).text());\n    });\n\n    console.log(titles);\n  } catch (error) {\n    console.error(`Error scraping data: ${error}`);\n  }\n}\n\n\/\/ Example URL\nscrapeData('https:\/\/example-blog.com');<\/textarea><\/pre><span class=\"cbp-btn-text\">Copy<\/span><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">axios<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">require<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;axios&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">cheerio<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">require<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;cheerio&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #908CAA; font-style: italic\">\/\/<\/span><span style=\"color: #6E6A86; font-style: italic\"> Function to fetch HTML<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">async<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">fetchHTML<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #C4A7E7; font-style: italic\">url<\/span><span style=\"color: #908CAA\">)<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">try<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">await<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">axios<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">get<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #E0DEF4; font-style: italic\">url<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">catch<\/span><span style=\"color: #E0DEF4\"> (<\/span><span style=\"color: #E0DEF4; font-style: italic\">error<\/span><span style=\"color: #E0DEF4\">) <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #E0DEF4; font-style: italic\">console<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">error<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">`Error fetching HTML: <\/span><span style=\"color: #908CAA\">${<\/span><span style=\"color: #E0DEF4; font-style: italic\">error<\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #F6C177\">`<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">throw<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">error<\/span><span style=\"color: #908CAA\">;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA; font-style: italic\">\/\/<\/span><span style=\"color: #6E6A86; font-style: italic\"> Rethrow the error to ensure calling functions can handle it<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #908CAA; font-style: italic\">\/\/<\/span><span style=\"color: #6E6A86; font-style: italic\"> Function to scrape data<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">async<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">scrapeData<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #C4A7E7; font-style: italic\">url<\/span><span style=\"color: #908CAA\">)<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">try<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">html<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">await<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">fetchHTML<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #E0DEF4; font-style: italic\">url<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">$<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">cheerio<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">load<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #E0DEF4; font-style: italic\">html<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">    <\/span><span style=\"color: #908CAA; font-style: italic\">\/\/<\/span><span style=\"color: #6E6A86; font-style: italic\"> Example: Extracting titles from a blog<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">titles<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> []<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #EA9A97\">$<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;h2.post-title&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">each<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #C4A7E7; font-style: italic\">index<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #C4A7E7; font-style: italic\">element<\/span><span style=\"color: #908CAA\">)<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=&gt;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">      <\/span><span style=\"color: #E0DEF4; font-style: italic\">titles<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">push<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #EA9A97\">$<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #E0DEF4; font-style: italic\">element<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">text<\/span><span style=\"color: #E0DEF4\">())<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #E0DEF4; font-style: italic\">console<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">log<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #E0DEF4; font-style: italic\">titles<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">catch<\/span><span style=\"color: #E0DEF4\"> (<\/span><span style=\"color: #E0DEF4; font-style: italic\">error<\/span><span style=\"color: #E0DEF4\">) <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #E0DEF4; font-style: italic\">console<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">error<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">`Error scraping data: <\/span><span style=\"color: #908CAA\">${<\/span><span style=\"color: #E0DEF4; font-style: italic\">error<\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #F6C177\">`<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #908CAA; font-style: italic\">\/\/<\/span><span style=\"color: #6E6A86; font-style: italic\"> Example URL<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EA9A97\">scrapeData<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;https:\/\/example-blog.com&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Scraping Dynamic Web Pages<\/h4>\n\n\n\n<p>For dynamic web pages that load content via JavaScript, Puppeteer is an excellent tool:<\/p>\n\n\n\n<p>Launching the Browser and Navigate to Page:<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e0def4;--cbp-line-number-width:calc(2 * 0.6 * .75rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#232136;display:none;background-color:#e0def4\" aria-label=\"Copy\" data-copied-text=\"Copied!\" data-has-text-button=\"textSimple\" data-inside-header-type=\"none\" aria-live=\"polite\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>const puppeteer = require('puppeteer');\n\nasync function scrapeDynamicContent(url) {\n  let browser;\n  try {\n    browser = await puppeteer.launch();\n    const page = await browser.newPage();\n    await page.goto(url, { waitUntil: 'networkidle2' });\n\n    \/\/ Example: Extracting titles from a blog\n    const titles = await page.evaluate(() => {\n      return Array.from(document.querySelectorAll('h2.post-title')).map(el => el.innerText);\n    });\n\n    console.log(titles);\n  } catch (error) {\n    console.error(`Error scraping dynamic content: ${error}`);\n  } finally {\n    if (browser) {\n      await browser.close();\n    }\n  }\n}\n\n\/\/ Example URL\nscrapeDynamicContent('https:\/\/example-dynamic-blog.com'); <\/textarea><\/pre><span class=\"cbp-btn-text\">Copy<\/span><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">puppeteer<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">require<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;puppeteer&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">async<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">scrapeDynamicContent<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #C4A7E7; font-style: italic\">url<\/span><span style=\"color: #908CAA\">)<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">let<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">browser<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">try<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #E0DEF4; font-style: italic\">browser<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">await<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">puppeteer<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">launch<\/span><span style=\"color: #E0DEF4\">()<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">page<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">await<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">browser<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">newPage<\/span><span style=\"color: #E0DEF4\">()<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">await<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">page<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">goto<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #E0DEF4; font-style: italic\">url<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4\"> waitUntil<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;networkidle2&#39;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">    <\/span><span style=\"color: #908CAA; font-style: italic\">\/\/<\/span><span style=\"color: #6E6A86; font-style: italic\"> Example: Extracting titles from a blog<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">titles<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">await<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">page<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">evaluate<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #908CAA\">()<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=&gt;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">      <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">Array<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">from<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #E0DEF4; font-style: italic\">document<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">querySelectorAll<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;h2.post-title&#39;<\/span><span style=\"color: #E0DEF4\">))<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">map<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #C4A7E7; font-style: italic\">el<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=&gt;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">el<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #E0DEF4; font-style: italic\">innerText<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #E0DEF4; font-style: italic\">console<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">log<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #E0DEF4; font-style: italic\">titles<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">catch<\/span><span style=\"color: #E0DEF4\"> (<\/span><span style=\"color: #E0DEF4; font-style: italic\">error<\/span><span style=\"color: #E0DEF4\">) <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #E0DEF4; font-style: italic\">console<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">error<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">`Error scraping dynamic content: <\/span><span style=\"color: #908CAA\">${<\/span><span style=\"color: #E0DEF4; font-style: italic\">error<\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #F6C177\">`<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">finally<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">if<\/span><span style=\"color: #E0DEF4\"> (<\/span><span style=\"color: #E0DEF4; font-style: italic\">browser<\/span><span style=\"color: #E0DEF4\">) <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">      <\/span><span style=\"color: #3E8FB0\">await<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">browser<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">close<\/span><span style=\"color: #E0DEF4\">()<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #908CAA; font-style: italic\">\/\/<\/span><span style=\"color: #6E6A86; font-style: italic\"> Example URL<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EA9A97\">scrapeDynamicContent<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;https:\/\/example-dynamic-blog.com&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><span style=\"color: #E0DEF4\"> <\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized centered\"><img decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/data-handling-and-storage-1024x576.png\" alt=\"A drawing of a person looking through a filing cabinet with the title &quot;Data Handling and Storage&quot;.\" class=\"wp-image-57153\" style=\"object-fit:cover\" srcset=\"https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/data-handling-and-storage-1024x576.png 1024w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/data-handling-and-storage-300x169.png 300w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/data-handling-and-storage-768x432.png 768w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/data-handling-and-storage-1536x864.png 1536w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/data-handling-and-storage-600x338.png 600w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/data-handling-and-storage.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Data Handling and Storage<\/h2>\n\n\n\n<p>Once you\u2019ve scraped everything, you would need a way to handle and store the data. This will help you stay organized and keep your data accessible and usable for analysis. We will be covering the lines of code you need to include within your script to ensure your data is saved in <a href=\"https:\/\/proxidize.com\/blog\/json-vs-csv\/\" data-type=\"link\" data-id=\"https:\/\/proxidize.com\/blog\/json-vs-csv\/\">either JSON, CSV<\/a>, or a collection of databases, depending on your specific need.<\/p>\n\n\n\n<p>If you wish to store your data hierarchically, we suggest you store them as JSON files. If you wish to store the data in organized tables, CSV would be your best bet. However, if you have complex and larger-scale data, it is recommended to store them in database tables such as MySQL or NoSQL.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Storing Data in Files<\/h3>\n\n\n\n<p>Here is the code required to store data in your preferred format, depending on the size of your scraping project.&nbsp;<\/p>\n\n\n\n<p>For JSON:<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e0def4;--cbp-line-number-width:calc(2 * 0.6 * .75rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#232136;display:none;background-color:#e0def4\" aria-label=\"Copy\" data-copied-text=\"Copied!\" data-has-text-button=\"textSimple\" data-inside-header-type=\"none\" aria-live=\"polite\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>const fs = require('fs').promises;\n\nconst data = &#91;\n  { title: 'Post 1', author: 'Author 1', date: '2024-08-02' },\n  { title: 'Post 2', author: 'Author 2', date: '2024-08-03' },\n&#93;;\n\nasync function writeDataToFile() {\n  try {\n    await fs.writeFile('data.json', JSON.stringify(data, null, 2));\n    console.log('Data has been written to JSON file.');\n  } catch (err) {\n    console.error('Error writing to file', err);\n  }\n}\n\nwriteDataToFile(); <\/textarea><\/pre><span class=\"cbp-btn-text\">Copy<\/span><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">fs<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">require<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;fs&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #E0DEF4; font-style: italic\">promises<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> &#91;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4\"> title<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;Post 1&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> author<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;Author 1&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> date<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;2024-08-02&#39;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">},<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4\"> title<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;Post 2&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> author<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;Author 2&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> date<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;2024-08-03&#39;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">},<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">&#93;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">async<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">writeDataToFile<\/span><span style=\"color: #908CAA\">()<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">try<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">await<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">fs<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">writeFile<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;data.json&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">JSON<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">stringify<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">null<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">2<\/span><span style=\"color: #E0DEF4\">))<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #E0DEF4; font-style: italic\">console<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">log<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;Data has been written to JSON file.&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">catch<\/span><span style=\"color: #E0DEF4\"> (<\/span><span style=\"color: #E0DEF4; font-style: italic\">err<\/span><span style=\"color: #E0DEF4\">) <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #E0DEF4; font-style: italic\">console<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">error<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;Error writing to file&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">err<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #EA9A97\">writeDataToFile<\/span><span style=\"color: #E0DEF4\">()<\/span><span style=\"color: #908CAA\">;<\/span><span style=\"color: #E0DEF4\"> <\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>For CSV files:<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e0def4;--cbp-line-number-width:calc(2 * 0.6 * .75rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#232136;display:none;background-color:#e0def4\" aria-label=\"Copy\" data-copied-text=\"Copied!\" data-has-text-button=\"textSimple\" data-inside-header-type=\"none\" aria-live=\"polite\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>const fs = require('fs').promises;\nconst { parse } = require('json2csv');\n\nconst data = &#91;\n  { title: 'Post 1', author: 'Author 1', date: '2024-08-02' },\n  { title: 'Post 2', author: 'Author 2', date: '2024-08-03' },\n&#93;;\n\nconst csv = parse(data);\n\nasync function writeDataToCSV() {\n  try {\n    await fs.writeFile('data.csv', csv);\n    console.log('Data has been written to CSV file.');\n  } catch (err) {\n    console.error('Error writing to CSV file', err);\n  }\n}\n\nwriteDataToCSV();\n<\/textarea><\/pre><span class=\"cbp-btn-text\">Copy<\/span><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">fs<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">require<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;fs&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #E0DEF4; font-style: italic\">promises<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">parse<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">require<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;json2csv&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> &#91;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4\"> title<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;Post 1&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> author<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;Author 1&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> date<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;2024-08-02&#39;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">},<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4\"> title<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;Post 2&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> author<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;Author 2&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> date<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;2024-08-03&#39;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">},<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">&#93;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">csv<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">parse<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">async<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">writeDataToCSV<\/span><span style=\"color: #908CAA\">()<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">try<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">await<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">fs<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">writeFile<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;data.csv&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">csv<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #E0DEF4; font-style: italic\">console<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">log<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;Data has been written to CSV file.&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">catch<\/span><span style=\"color: #E0DEF4\"> (<\/span><span style=\"color: #E0DEF4; font-style: italic\">err<\/span><span style=\"color: #E0DEF4\">) <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #E0DEF4; font-style: italic\">console<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">error<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;Error writing to CSV file&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">err<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #EA9A97\">writeDataToCSV<\/span><span style=\"color: #E0DEF4\">()<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>For MySQL:<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e0def4;--cbp-line-number-width:calc(2 * 0.6 * .75rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#232136;display:none;background-color:#e0def4\" aria-label=\"Copy\" data-copied-text=\"Copied!\" data-has-text-button=\"textSimple\" data-inside-header-type=\"none\" aria-live=\"polite\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>const mysql = require('mysql2\/promise');\n\nconst data = [\n  &#91;'Post 1', 'Author 1', '2024-08-02'&#93;,\n  &#91;'Post 2', 'Author 2', '2024-08-03'&#93;,\n];\n\nasync function insertData() {\n  const connection = await mysql.createConnection({\n    host: 'localhost',\n    user: 'root',\n    password: 'password',\n    database: 'web_scraping',\n  });\n\n  try {\n    const query = 'INSERT INTO posts (title, author, date) VALUES ?';\n    const &#91;result&#93; = await connection.query(query, &#91;data&#93;);\n    console.log('Data has been inserted into the database.');\n  } catch (err) {\n    console.error('Error inserting data into the database:', err);\n  } finally {\n    await connection.end();\n  }\n}\n\ninsertData();\n<\/textarea><\/pre><span class=\"cbp-btn-text\">Copy<\/span><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">mysql<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">require<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;mysql2\/promise&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> [<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  &#91;<\/span><span style=\"color: #F6C177\">&#39;Post 1&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;Author 1&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;2024-08-02&#39;<\/span><span style=\"color: #E0DEF4\">&#93;<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  &#91;<\/span><span style=\"color: #F6C177\">&#39;Post 2&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;Author 2&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;2024-08-03&#39;<\/span><span style=\"color: #E0DEF4\">&#93;<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">]<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">async<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">insertData<\/span><span style=\"color: #908CAA\">()<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">connection<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">await<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">mysql<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">createConnection<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    host<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;localhost&#39;<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    user<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;root&#39;<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    password<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;password&#39;<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    database<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;web_scraping&#39;<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">try<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">query<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;INSERT INTO posts (title, author, date) VALUES ?&#39;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">&#91;<\/span><span style=\"color: #E0DEF4; font-style: italic\">result<\/span><span style=\"color: #908CAA\">&#93;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">await<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">connection<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">query<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #E0DEF4; font-style: italic\">query<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> &#91;<\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #E0DEF4\">&#93;)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #E0DEF4; font-style: italic\">console<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">log<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;Data has been inserted into the database.&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">catch<\/span><span style=\"color: #E0DEF4\"> (<\/span><span style=\"color: #E0DEF4; font-style: italic\">err<\/span><span style=\"color: #E0DEF4\">) <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #E0DEF4; font-style: italic\">console<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">error<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;Error inserting data into the database:&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">err<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">finally<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">await<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">connection<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">end<\/span><span style=\"color: #E0DEF4\">()<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #EA9A97\">insertData<\/span><span style=\"color: #E0DEF4\">()<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>For NoSQL:<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e0def4;--cbp-line-number-width:calc(2 * 0.6 * .75rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#232136;display:none;background-color:#e0def4\" aria-label=\"Copy\" data-copied-text=\"Copied!\" data-has-text-button=\"textSimple\" data-inside-header-type=\"none\" aria-live=\"polite\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>const { MongoClient } = require('mongodb');\n\nconst url = 'mongodb:\/\/localhost:27017';\nconst client = new MongoClient(url);\n\nasync function run() {\n  try {\n    await client.connect();\n    console.log('Connected to MongoDB');\n\n    const database = client.db('web_scraping');\n    const collection = database.collection('posts');\n\n    const data = &#91;\n      { title: 'Post 1', author: 'Author 1', date: '2024-08-02' },\n      { title: 'Post 2', author: 'Author 2', date: '2024-08-03' },\n    &#93;;\n\n    const result = await collection.insertMany(data);\n    console.log(`${result.insertedCount} documents were inserted.`);\n  } catch (err) {\n    console.error('An error occurred while inserting documents:', err);\n  } finally {\n    await client.close();\n    console.log('Disconnected from MongoDB');\n  }\n}\n\nrun().catch(err => console.error('An unexpected error occurred:', err));<\/textarea><\/pre><span class=\"cbp-btn-text\">Copy<\/span><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">MongoClient<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">require<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;mongodb&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">url<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;mongodb:\/\/localhost:27017&#39;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">client<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">new<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">MongoClient<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #E0DEF4; font-style: italic\">url<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">async<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">run<\/span><span style=\"color: #908CAA\">()<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">try<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">await<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">client<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">connect<\/span><span style=\"color: #E0DEF4\">()<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #E0DEF4; font-style: italic\">console<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">log<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;Connected to MongoDB&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">database<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">client<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">db<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;web_scraping&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">collection<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">database<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">collection<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;posts&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> &#91;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">      <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4\"> title<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;Post 1&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> author<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;Author 1&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> date<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;2024-08-02&#39;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">},<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">      <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4\"> title<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;Post 2&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> author<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;Author 2&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> date<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;2024-08-03&#39;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">},<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    &#93;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">result<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">await<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">collection<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">insertMany<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #E0DEF4; font-style: italic\">console<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">log<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">`<\/span><span style=\"color: #908CAA\">${<\/span><span style=\"color: #E0DEF4; font-style: italic\">result<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #E0DEF4; font-style: italic\">insertedCount<\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #F6C177\"> documents were inserted.`<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">catch<\/span><span style=\"color: #E0DEF4\"> (<\/span><span style=\"color: #E0DEF4; font-style: italic\">err<\/span><span style=\"color: #E0DEF4\">) <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #E0DEF4; font-style: italic\">console<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">error<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;An error occurred while inserting documents:&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">err<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">finally<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">await<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">client<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">close<\/span><span style=\"color: #E0DEF4\">()<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #E0DEF4; font-style: italic\">console<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">log<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;Disconnected from MongoDB&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #EA9A97\">run<\/span><span style=\"color: #E0DEF4\">()<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">catch<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #C4A7E7; font-style: italic\">err<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=&gt;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">console<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">error<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;An unexpected error occurred:&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">err<\/span><span style=\"color: #E0DEF4\">))<\/span><span style=\"color: #908CAA\">;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized centered\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/enhancing-web-scraping-with-javascript-1024x576.png\" alt=\"A drawing of robots working on a computer displaying the JavaScript logo with the title &quot;Enhancing Web Scraping with JavaScript&quot;.\" class=\"wp-image-57154\" style=\"object-fit:cover\" srcset=\"https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/enhancing-web-scraping-with-javascript-1024x576.png 1024w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/enhancing-web-scraping-with-javascript-300x169.png 300w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/enhancing-web-scraping-with-javascript-768x432.png 768w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/enhancing-web-scraping-with-javascript-1536x864.png 1536w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/enhancing-web-scraping-with-javascript-600x338.png 600w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/08\/enhancing-web-scraping-with-javascript.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Enhancing Web Scraping with JavaScript<\/h2>\n\n\n\n<p>There are a few things to keep in mind that could make web scraping with JavaScript easier and more efficient. One of the biggest challenges most web scrapers face is getting their IP blocked. This could be due to a heavy amount of requests being sent to the website. When a website notices this, they will automatically ban the IP at risk of having their servers overloaded and risk shutting the website down. Similarly, not every website is fond of web scraping. To ensure the website is comfortable with scraping or at the very least, to find out which information a website is comfortable being scraped, you should check the robot.txt rules. You can find this out by typing the slug \u201crobots.txt\u201d to your website URL (eg: www.google.com\/robot.txt).&nbsp;<\/p>\n\n\n\n<p>As for the IP bans, it is highly recommended that you use a <a href=\"https:\/\/proxidize.com\/proxy-server\">proxy server<\/a>, specifically a mobile proxy server. By using a proxy, your real IP address will remain hidden, allowing you to scrape without the risk of the IP address being detected, causing the website to ban you from accessing its content. The reason we specify a mobile proxy is that <a href=\"https:\/\/proxidize.com\/proxy-server\/mobile-proxies\/\">mobile proxy IPs<\/a> have the lowest chance of being detected as they ping the connection off of cell towers which have a slew of IP addresses being used by many people and the risk of banning one of these IP addresses could risk preventing a potential customer from accessing the website. Most mobile proxy providers additionally offer a rotating IP option which would come in handy when web scraping as with a rotating IP, the scraped website would be unable to detect that a single user is performing the scraping action. This would ensure that your scraping tasks remain undetected as you gather all the pieces of information you desire.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Web Scraping with JavaScript is fairly straightforward once you understand the core components that make JavaScript an excellent language. This includes its ability to work with dynamic websites and its asynchronistic programming which allows you to multitask your scraping and use heavily documented libraries such as Axios, Cheerio, and Puppeteer. However, knowing how to scrape is only half the challenge. To scrape data without being blocked or detected, you could incorporate a mobile proxy to help hide and rotate your IP, thus making your actions seem as though the requests are coming from multiple users.&nbsp;<\/p>\n\n\n\n<p>With this guide, you should have gained an understanding of how to implement code for web scraping with JavaScript with a way to store your data in your preferred method. However, if you are a beginner, JavaScript does have a learning curve. If you wish to practice scraping in another language before tackling JavaScript, we recommend you check out our article on <a href=\"https:\/\/proxidize.com\/use-cases\/web-scraping-with-selenium\/\">Web Scraping with Selenium and Python<\/a> which should provide a base-level understanding of the functionality and framework of scraping. Once you find yourself more comfortable scraping with Python, you could return to this article and test yourself with one of the most popular and powerful programming languages available.&nbsp;<\/p>\n","protected":false},"author":2627,"featured_media":76287,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","format":"standard","categories":[110],"tags":[],"class_list":["post-57151","blog","type-blog","status-publish","format-standard","has-post-thumbnail","hentry","category-web-scraping-and-automation"],"acf":[],"_links":{"self":[{"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/blog\/57151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/types\/blog"}],"author":[{"embeddable":true,"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/users\/2627"}],"replies":[{"embeddable":true,"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/comments?post=57151"}],"version-history":[{"count":6,"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/blog\/57151\/revisions"}],"predecessor-version":[{"id":84903,"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/blog\/57151\/revisions\/84903"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/media\/76287"}],"wp:attachment":[{"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/media?parent=57151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/categories?post=57151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/tags?post=57151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}