简体   繁体   English

为什么我总是得到 Null?

[英]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!我正在尝试创建一个 chrome 扩展来从 RatemyProffessor 中抓取数据,但是当试图从上面的 url 中抓取最有意义的评论时,我一直为空,任何帮助都会很棒!

When I say "getting Null" I mean console.log(profTopComment.html()) is giving me null in the terminal.当我说“获取空值”时,我的意思是 console.log(profTopComment.html()) 在终端中给我空值。

I am trying to scrape Most Helpful Rating.我正在尝试抓取最有用的评级。

  • '.Comments__StyledComments-dzzyvm-0.dvnRbr' the second "." '.Comments__StyledComments-dzzyvm-0.dvnRbr'第二个"." is needed to look for a <div></div> element with two different classes on it需要查找具有two不同类的<div></div>元素
  • Basically just change '.Comments__StyledComments-dzzyvm-0 dvnRbr' to '.Comments__StyledComments-dzzyvm-0.dvnRbr' in your code.基本上只需将代码中的'.Comments__StyledComments-dzzyvm-0 dvnRbr'更改为'.Comments__StyledComments-dzzyvm-0 dvnRbr' '.Comments__StyledComments-dzzyvm-0.dvnRbr'

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在此三元分配中我总是保持空值? - Why do I keep getting null in this ternary assignment? 为什么我一直在控制台日志中保持null? - Why do I keep on getting null in the console log? 为什么为什么不断收到“无法调用null的方法&#39;addEventListener&#39;”? - Why do I keep getting “Cannot call method 'addEventListener' of null”? 为什么我不断收到此错误? 未捕获的类型错误:无法读取 null 的属性(读取“术语”) - why do i keep getting this error? Uncaught TypeError: Cannot read properties of null (reading 'term') 为什么我不断收到此错误? 未捕获的类型错误:无法读取 null 的属性“classList” - Why do I keep getting this error? Uncaught TypeError: Cannot read property 'classList' of null 为什么我不断收到ReferenceError:*未定义? - Why do I keep getting ReferenceError: * is not defined? 为什么我不断得到NaN? - Why Do I keep getting NaN? 为什么我不断得到$(...)。scrollSpy是未定义的? - Why do I keep getting $(…).scrollSpy is undefined? 为什么我通过此JSONP feed不断收到“未定义”错误? - Why do I keep getting an “undefined” error with this JSONP feed? 为什么我不断收到TypeError:用户不是构造函数? - Why do I keep getting TypeError: User is not a constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM