如何修复 Node/Cheerio 中的“$(...).click 不是函数”
How to fix '$(...).click is not a function' in Node/Cheerio
我正在 node.js 中编写一个应用程序,它将导航到一个网站,单击该网站上的一个按钮,然后从该网站提取某些数据。除了单击按钮方面外,一切进展顺利。我似乎无法模拟按钮点击。我对此非常陌生,所以我很感激你们的任何建议!遗憾的是,我已经在互联网上搜索以寻找解决此问题的方法,但一直找不到。
我在使用 'request' 和 'cheerio'.
的 .js 文件中使用了 .click()
和 .bind('click, ...)
我还尝试在另一个使用 'chrome-launcher'、'chrome-remote-interface' 和 'puppeteer' 的 .js 文件中使用 page.click()
和 page.evaluate()
。
这是我的 'request' 和 'cheerio' 文件的代码:
const request = require('request');
const cheerio = require('cheerio');
let p1 = {}, p2 = {}, p3 = {}, p4 = {}, p5 = {};
p1.name = 'TheJackal666';
p2.name = 'Naether Raviel';
p3.name = 'qman37';
p4.name = 'ranger51';
p5.name = 'fernanda12x';
const team = {1: p1, 2: p2, 3: p3, 4: p4, 5: p5};
for(var x in team){
let url = 'https://na.op.gg/summoner/userName=' +
team[x].name;
request(url, (error, response, html) => {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(html);
$('.SummonerRefreshButton.Button.SemiRound.Blue').click();
//FIXME: MAKE A FUNCTION THAT SUCCESSFULLY "CLICKS" UPDATE BUTTON
team[x].overallWR = $('.winratio');
team[x].overallWR =
team[x].overallWR.text().match(/\d/g);
team[x].overallWR =
team[x].overallWR.join("");
console.log(team[x].overallWR);
}
});
}
我希望能成功点击任何页面上的更新按钮(页面上有一个部分显示上次更新时间)而不会出现错误。事实上,我要么得到一个错误:
"$(...).click is not a function"
或者(如果我将该行合并到外部函数中)我没有收到错误,但没有结果。
Cheerio is not a web browser
Cheerio parses markup and provides an API for traversing/manipulating the resulting data structure. It does not interpret the result as a web browser does. Specifically, it does not produce a visual rendering, apply CSS, load external resources, or execute JavaScript. If your use case requires any of this functionality, you should consider projects like PhantomJS or JSDom.
Cheerio 是一个 HTML 解析器。
Cheerio 可用于 select 和操作 dom 元素,但它不是一个完整的浏览器。
Cheerio 只能访问原始来源 dom,这意味着如果网页的 dom 被 javascript 操纵,Cheerio 将不会注意到该更改。
Cheerio 不能用于与 dom 元素 (ala jQuery) 交互,因为它不会在 window (js window)
目前,如果您需要操纵或 select 反对 js-rendered html,您最好的选择是操纵木偶。这可能会改变,
HTH
我正在 node.js 中编写一个应用程序,它将导航到一个网站,单击该网站上的一个按钮,然后从该网站提取某些数据。除了单击按钮方面外,一切进展顺利。我似乎无法模拟按钮点击。我对此非常陌生,所以我很感激你们的任何建议!遗憾的是,我已经在互联网上搜索以寻找解决此问题的方法,但一直找不到。
我在使用 'request' 和 'cheerio'.
的 .js 文件中使用了.click()
和 .bind('click, ...)
我还尝试在另一个使用 'chrome-launcher'、'chrome-remote-interface' 和 'puppeteer' 的 .js 文件中使用 page.click()
和 page.evaluate()
。
这是我的 'request' 和 'cheerio' 文件的代码:
const request = require('request');
const cheerio = require('cheerio');
let p1 = {}, p2 = {}, p3 = {}, p4 = {}, p5 = {};
p1.name = 'TheJackal666';
p2.name = 'Naether Raviel';
p3.name = 'qman37';
p4.name = 'ranger51';
p5.name = 'fernanda12x';
const team = {1: p1, 2: p2, 3: p3, 4: p4, 5: p5};
for(var x in team){
let url = 'https://na.op.gg/summoner/userName=' +
team[x].name;
request(url, (error, response, html) => {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(html);
$('.SummonerRefreshButton.Button.SemiRound.Blue').click();
//FIXME: MAKE A FUNCTION THAT SUCCESSFULLY "CLICKS" UPDATE BUTTON
team[x].overallWR = $('.winratio');
team[x].overallWR =
team[x].overallWR.text().match(/\d/g);
team[x].overallWR =
team[x].overallWR.join("");
console.log(team[x].overallWR);
}
});
}
我希望能成功点击任何页面上的更新按钮(页面上有一个部分显示上次更新时间)而不会出现错误。事实上,我要么得到一个错误:
"$(...).click is not a function"
或者(如果我将该行合并到外部函数中)我没有收到错误,但没有结果。
Cheerio is not a web browser
Cheerio parses markup and provides an API for traversing/manipulating the resulting data structure. It does not interpret the result as a web browser does. Specifically, it does not produce a visual rendering, apply CSS, load external resources, or execute JavaScript. If your use case requires any of this functionality, you should consider projects like PhantomJS or JSDom.
Cheerio 是一个 HTML 解析器。
Cheerio 可用于 select 和操作 dom 元素,但它不是一个完整的浏览器。
Cheerio 只能访问原始来源 dom,这意味着如果网页的 dom 被 javascript 操纵,Cheerio 将不会注意到该更改。
Cheerio 不能用于与 dom 元素 (ala jQuery) 交互,因为它不会在 window (js window)
目前,如果您需要操纵或 select 反对 js-rendered html,您最好的选择是操纵木偶。这可能会改变,
HTH