如何修复 Node/Cheerio 中的“$(...).click is not a function" [英] How to fix '$(...).click is not a function' in Node/Cheerio

查看:22
本文介绍了如何修复 Node/Cheerio 中的“$(...).click is not a function"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 node.js 编写一个应用程序,它将导航到一个网站,点击网站上的一个按钮,然后从网站中提取某些数据.除了单击按钮方面外,一切都进行得很顺利.我似乎无法模拟按钮点击.我对此非常陌生,所以我很感激你们的任何建议!遗憾的是,我已经在互联网上寻找解决此问题的方法,但一直找不到.

I am writing an application in node.js that will navigate to a website, click a button on the website, and then extract certain pieces of data from the website. All is going well except for the button-clicking aspect. I cannot seem to simulate a button click. I'm extremely new at this, so I'd appreciate any suggestions y'all have! Sadly I've scoured the internet looking for a solution to this issue and have been unable to find one.

我在使用request"和cheerio"的 .js 文件中使用了 .click().bind('click, ...).

I have used .click() and .bind('click, ...) in a .js file that uses 'request' and 'cheerio'.

我还尝试在使用chrome-launcher"、chrome-"的不同 .js 文件中使用 page.click()page.evaluate()远程接口"和傀儡师".

I have also tried using page.click() and page.evaluate() in a different .js file that uses 'chrome-launcher', 'chrome-remote-interface', and 'puppeteer'.

这是我的请求"和cheerio"文件的代码:

Here is my code for the 'request' and 'cheerio' file:

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

我希望能够成功点击任何页面上的更新按钮(页面上有一个部分显示上次更新时间)而不会出错.事实上,我要么得到一个错误:

I expect to successfully click the update button on any of the pages (there is a section on the page that says when it was last updated) without getting an error. As it is, I either get an error that:

"$(...).click is not a function"

或者(如果我将该行合并到外部函数中)我没有错误,但没有结果.

or (if I incorporate that line into an outer function) I get no error, but no result.

推荐答案

参见 文档:

Cheerio 不是网络浏览器

Cheerio is not a web browser

Cheerio 解析标记并提供用于遍历/操作结果数据结构的 API.它不会像 Web 浏览器那样解释结果.具体来说,它不会产生视觉渲染、应用 CSS、加载外部资源或执行 JavaScript.如果您的用例需要任何此类功能,您应该考虑 PhantomJS 或 JSDom 之类的项目.

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.

这篇关于如何修复 Node/Cheerio 中的“$(...).click is not a function"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆