Learn how to extract IMG SRC using web scraping with cheerio in node.js

Implementing an event listener to FETCH this and using cheerio to extract the img src from:

<div class="mainimage"> 

Existing script:

var cheerio = require('cheerio'),
$ = cheerio.load(this.responseText);
console.log($('mainimage').attr('img'));

Unfortunately, it is returning 'undefined'

Answer №1

To begin, ensure you have the .mainimage class (don't forget the dot) as this is how jQuery selects elements with a specific class. Next, target the <img> tag within the .mainimage div. Lastly, retrieve the src attribute from the image tag:

console.log($('.mainimage img').attr('src'));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to transfer an ag-grid table using nodejs?

Within my webpage, I utilize the AG-GRID library to dynamically generate a table in the backend and create a corresponding JS file. This file is then called from the frontend where Handlebars is employed for rendering. Thus far, the code functions as inten ...

Using Node.js together with MQTT can sometimes result in errors like this: "Error: 'uncaughtException: Cannot set headers after they are sent to the client'"

Below is the code snippet for my fridge controller: const fs = require('fs'); const mqtt = require('mqtt'); const transporter = require('../params/mail') const winston = require('../params/log'); const User = require ...

What is the best way to include a pagination feature with numbered buttons at the bottom of my website for pages 1 through 4?

I am in the process of creating a website that allows users to access and download various files. My plan is to upload these files to the site, where they will be displayed on the homepage. However, I would like only the first 10 files to be visible on t ...

Creating a secure authentication system with Parse for password recovery and website logins

Our organization has chosen to utilize a Parse backend for managing user accounts, and I am tasked with creating web pages for functionalities like password reset that will seamlessly integrate with our mobile applications. It has been some time since I ha ...

Node Js issue stemming from unaddressed promises leading to rejection

Hi, everyone! I'm currently facing some challenges while trying to deploy this API. The error message I keep getting is: "UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error may have occurred due to either throwing inside an asyn ...

Having trouble executing nodemon on my ubuntu system due to a SyntaxError: Unexpected token

The command I am running is: nodemon server.js, and the error I am getting is: nodemon server.js /usr/local/lib/node_modules/nodemon/node_modules/update-notifier/index.js:58 } catch { ^ SyntaxError: Unexpected token { a ...

What is the best way to manage and modify one-to-many connections in Node.js?

I am currently developing a Node application utilizing mongoose, mongodb, and restify. I have a question regarding the best practice for importing code from various models when defining route functions. Your guidance on this matter will be greatly apprecia ...

Ways to utilize a single node_modules folder across multiple projects

Is there a simple method to centralize and share one node_modules folder among all Angular projects, thereby preventing the need to download the same dependencies each time we start a new project? If so, is this approach recommended (what are the pros and ...

Node.js: Leveraging Express to Compare URL Parameters and Handle Delete Requests with Array Values

Having some issues with the Express routes I set up for an array of objects and CRUD operations. Everything works smoothly except for the "Delete" operation. I am trying to delete an object by matching its URL parameter ID with the value of its key. Howev ...

What are the troubleshooting tools available in Electron for detecting errors and debugging issues?

What is the process for accessing error messages and console logs in Electron while developing? Can the logs be saved directly to a file as well? Edit: Similar to how error messages and console logs are shown in Chrome's dev tools, but within Electro ...

Issue with establishing socket connection between React and Express.js

When setting up the socket in app.js(backend), I follow this approach: var createError = require('http-errors'); var express = require('express'); var path = require('path'); var cookieParser = require('cookie-parser&apos ...

What is the proper way to import route modules in Express?

After using express-generator to create a project, I ended up with two files in my routes directory: index.js and users.js. Additionally, there was a file named about.js which was responsible for handling the /about route. However, when attempting to acce ...

Tips for adjusting the node js request timeout in the core configuration or the default library of node js

Within the realm of node js, I discovered that the Socket Timeout default setting is Two Minutes, as stipulated in the documentation. In order to make modifications, I am seeking the specific location within the package where this code resides. Although I ...

Simple steps to load various json files into separate json objects using node.js

I am new to working with Json and node.js My goal is to load a json file into a JsonObject using node.js, but I have been struggling to accomplish this task. I have created two files, one named server.js and the other jsonresponse.json. My objective is t ...

The error message "Unable to access characteristics of the object (specifically, the 'transformFile' property) due to it being undefined

After updating my node today, I encountered the following error: error: TypeError: Cannot read properties of undefined (reading 'transformFile') at Bundler.transformFile (/Users/.../node_modules/metro/src/Bundler.js:48:30) at runMicrotask ...

Using Node.js and Express to create a MySQL API that outputs a JSON array as a string

Currently working on setting up an Express API server to fetch data for a portfolio website. I've configured the MySQL table with a JSON data type for the 'images' column, which is supposed to contain multiple image links for a gallery. Howe ...

Employing nvm within the company's network restrictions

Due to the restrictions of the corporate firewall, I am unable to install Node JS via nvm. To work around this, I have decided to utilize Fiddler for proxying and attempted to configure the proxy in nvm with the following command: nvm proxy , where 8888 ...

Is it possible to retrieve the createdAt timestamp without displaying the 'GMT+0000 (Coordinated Universal Time)'?

After conducting an extensive search, I have yet to find a satisfactory answer. My goal is to configure it without including the CUT time. {name: "Registered:", value: `${user.createdAt}`}, {name: "Joined:", value: `${message.guild.joinedAt}`} Presently, ...

How to Pass Parameters in NodeJS MongoDB Aggregation Pipeline

Is there a way to pass parameters to the aggregation process? I am receiving parameters and attempting to use the $match operator, but the query is returning an empty array: app.get('/api/:name', function(req, res){ var name = req.params ...

Steps to fix the postinstall error in Angular CLI

Can anyone lend a hand in resolving the following error? npm ERR! errno -4058 npm ERR! @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2c4f40456c14021c021a">[email protected]</a> postinstall: `node ./bin/p ...