javascript - 为什么我总是得到 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());
}
});
我正在尝试创建一个 chrome 扩展来从 RatemyProffessor 中抓取数据,但是当尝试从上面的 url 中抓取最有意义的评论时,我一直得到空值,任何帮助都会很棒!
当我说“获取 Null”时,我的意思是 console.log(profTopComment.html()) 在终端中给了我 null 。
我正在尝试获取最有用的评分。
解决方案
'.Comments__StyledComments-dzzyvm-0.dvnRbr'
第二个"."
是需要寻找具有不同类的<div></div>
元素two
- 基本上只是在您的代码中更改
'.Comments__StyledComments-dzzyvm-0 dvnRbr'
为。'.Comments__StyledComments-dzzyvm-0.dvnRbr'
例子:
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();
推荐阅读
- performance - 站点在第一次加载非常慢的 IIS 调试
- neo4j - 是否可以在 CYPHER 中设置(复制)另一个节点的标签?
- reactjs - 在 React Hooks 中使用函数 useEffect() 的问题
- java - 如何从节点获取列表并在 recyclerView 中使用该列表从 firebase 获取数据
- javascript - 使用对象键值对动态创建变量?
- android - 我可以将 Firebase 更新通知发送到 Playstore 上早期没有 Firebase 连接的旧版应用程序吗?
- python - Python 烧瓶 - request.data.get
- assembly - nasm中的macro vs equ
- python - NameError:名称“xxxx”未定义
- java - 第一个文字游戏:打印行复制