Implement cheerio library in Vue.js application

I am looking to extract data from a website within a Vue application using Cheerio. However, I encountered the following error:

Uncaught (in promise) TypeError: $.find is not a function

Code

export default {
  name: "App",
  created() {
    this.fetchUrl();
  },
  methods: {
    fetchUrl() {
      axios
        .get("https://cors-anywhere.herokuapp.com/https://stackoverflow.com/")
        .then(response => {
          const $ = cheerio.load(response.data);
          const span = $.find(".fs-headline2");
          console.log(span);
        });
    }
  }
};

Sandbox

In this scenario, my goal is to scrape the title 'For developers, by developers' from the homepage of Stack Overflow.

Visit CodeSandbox for more details

Answer №1

It appears that the method $.find does not exist, but there is a different method called $().find(). To locate all elements with the class .fs-headline2, use $('.fs-headline2') instead:

const $ = cheerio.load(response.data);
const span = $(".fs-headline2"); 

Check out the updated codesandbox here

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 could be causing this `even` function to malfunction when utilizing getElementById?

Need assistance with utilizing document.getElementById? Let's take a look at this code snippet: function even() for (i = 0; i < 10; i++) { if (i % 2 == 0) { alert(i); } } document.getElementById("even").innerHTML = i + &apos ...

Executing a jQuery script on various elements of identical types containing different content (sizes)

I am currently working on a jQuery script that will center my images based on the text block next to them. Since I am using foundation 5, I have to use a jQuery script to override the CSS instead of using vertical-align: middle. The script looks like thi ...

Can an array key be a function's return value used in PHP?

I attempted to create a utility function in a rushed manner that I wanted to use as the key for an array, but encountered syntax errors. Is it feasible to insert a function as a key within an array? function generateCustomKey(attr, value) { return &a ...

Unable to handle JQuery POST to PHP in success function

I am struggling with a jQuery post function that is supposed to call a PHP script in order to retrieve a value from the database. Although I can see in Firebug that the PHP file is being called and returning a 200 OK status, the success function in my JS ...

Steps to dynamically display or conceal a DIV using Bootstrap 5

I am facing an issue with a navbar that has buttons to reveal hidden divs underneath. <a data-bs-toggle="offcanvas" href="#select" role="button" aria-controls="select"></a> <div id="select" c ...

The DiscordBot is configured to send a personalized direct message to users who have chosen a specific role

Having trouble setting up my bot to send a DM to new members who choose the Advertising role, and I can't figure out why. I'm fairly new to this. const { Client, GatewayIntentBits } = require('discord.js'); const client = new Client({ ...

How can nested json be sorted effectively based on two specific fields?

Example Data: [{ 'ID': objID(abc123), 'Department': 'IT', 'Employees': [ { 'ID': 3, 'StartDate': '24-12-2022T08:30', 'active': true }, { ...

Using TypeScript with Node.js and Sequelize - the process of converting a value to a number and then back to a string within a map function using the OR

Currently, I am facing a challenge in performing addition on currency prices stored as an array of objects. The issue arises from the fact that the currency type can vary among 3 different types within the array of objects. The main hurdle I encounter is ...

Retrieving the value of the option that has been selected from a dropdown

I have successfully implemented a drop-down menu with the following code: // Creating a select element for priority within a form in the bottom row div const formPriority = document.createElement("select"); formPriority.setAttribute("name","project"); form ...

What is the process for integrating php script within javascript?

Is it possible to incorporate php scripts into javascript[1] in the same manner as it can be done in html[2]? server.php: <?php echo 'hello World'; ?> client.js (or client.php if needed): function foo() { var i = <?php include ...

Identifying the element with the specified ID within the table's <th> tag in JavaScript

I want to create a table with the title: Summary Statement as of August 3, 2017 at 12:55 The date is functioning properly using JavaScript in other parts of the code, but not in this section. Is there a specific technique I should be aware of? The rest of ...

Trouble with the Sendhub API

I'm currently working with the sendhub API and encountering an error. The error message states that the specified format 'application/x-www-form-urlencoded' does not have a deserialization method available. It is recommended to double-check ...

Use PHP to redirect a webpage as an alternative to utilizing JavaScript

Recently, I encountered an issue where I created a form on inde1.php and utilized JavaScript to post data to login.php. My current goal is to redirect the user to another page once login.php has been successfully executed. Below is the JavaScript code on ...

Having trouble loading a base64 image into a React component using webpack

In my images folder, I've created a file called b64Images.js which contains the following: export const placeholder = "data:image/png;base64,longb64string" I'm attempting to import this file into one of my react components using: import { plac ...

How can you pass an authorization token in a Next.js post request when using Django REST framework?

Is there a way to successfully pass a Django authorization token in Next.js using Axios? I attempted this method, but encountered a 404 error. let token = "Token 8736be9dba6ccb11208a536f3531bccc686cf88d" await axios.post(url,{ headers ...

What is the best way to refresh a single component in my application without causing the other components to reload?

I've been working on a review application with Vue.js that fetches random facts from an API (https://uselessfacts.jsph.pl/random.json?language=en) and allows users to provide feedback through radio buttons and text inputs. Once submitted, the feedback ...

Connect your Nuxt.js application with Mailchimp for seamless integration

I've tried the solution recommended in this thread, but unfortunately, it's not working for me. Every time I run npm run dev, I encounter an error message: ERROR - Compilation failed with 1 error at 11:21:24 The following dependency was not fo ...

JavaScript - Navigating through JSON object in reverse (from leaf to root) direction

FamilyTree= { "name":"Family", "photo":"images/family.jpg", "members":[ { "name":"Parent", "photo":"images/parent.jpg", "relationships":[ { "name":"Spouse", "photo":"images/spouse.jpg" }, ...

The toggle for hiding and showing, along with changing the button color, is not functioning properly due to

I'm encountering a puzzling issue with a toggle that hides and displays information and changes color on click. The functionality works flawlessly on the page where I initially wrote the code. For instance, the button's background shifts colors w ...

Exploring the world of Angular JS with its powerful directives, the magic of ng-repeat functionality,

Hello all, I am new to Angular so please bear with me if I make any mistakes in the terminology. Below is a simple example where I am trying to bind a template in a directive within an ng-repeat. When updating the input field, {{list.name}} gets updated b ...