简体   繁体   中英

Why do I keep getting Null?

const request = require('request');
const cheerio = require('cheerio');

request('https://www.ratemyprofessors.com/ShowRatings.jsp?tid=1985428', (error, response, html) => {
    if(!error && response.statusCode ==200){
        //console.log(html);
        const $ = cheerio.load(html); 
        
        const profTopComment = $('.Comments__StyledComments-dzzyvm-0 dvnRbr');
        
        console.log(profTopComment.html());
    }
});

I'm trying to create a chrome extension to scrape data from RatemyProffessor but when trying to scrape the most meaningful comment from the url above, I keep getting null, any help would be awesome!

When I say "getting Null" I mean console.log(profTopComment.html()) is giving me null in the terminal.

I am trying to scrape Most Helpful Rating.

  • '.Comments__StyledComments-dzzyvm-0.dvnRbr' the second "." is needed to look for a <div></div> element with two different classes on it
  • Basically just change '.Comments__StyledComments-dzzyvm-0 dvnRbr' to '.Comments__StyledComments-dzzyvm-0.dvnRbr' in your code.

Example:

const axios = require('axios');
const cheerio = require('cheerio');

async function testFunc() {
  const result = await 
  axios.get('https://www.ratemyprofessors.com/ShowRatings.jsp?tid=1985428');
  const $ = cheerio.load(result.data);
  const profTopComment = $('.Comments__StyledComments-dzzyvm-0.dvnRbr');
  console.log(profTopComment.html());
}
testFunc();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM