简体   繁体   English

如何修复Node / Cheerio中的'$(….click不是函数)'

[英]How to fix '$(…).click is not a function' in Node/Cheerio

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. 我正在node.js中编写一个应用程序,该应用程序将导航到一个网站,单击该网站上的一个按钮,然后从该网站中提取某些数据。 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. 可悲的是,我搜寻了互联网,以寻找解决此问题的方法,但找不到。

I have used .click() and .bind('click, ...) in a .js file that uses 'request' and 'cheerio'. 我已经在使用'request'和'cheerio'的.js文件中使用了.click .click().bind('click, ...)

I have also tried using page.click() and page.evaluate() in a different .js file that uses 'chrome-launcher', 'chrome-remote-interface', and 'puppeteer'. 我还尝试在使用'chrome-launcher','chrome-remote-interface'和' page.evaluate() '的其他.js文件中使用page.click()page.evaluate()

Here is my code for the 'request' and 'cheerio' file: 这是我的“ 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);
            }
        });
}

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. 或者(如果我将该行合并到外部函数中),则不会出错,但不会有结果。

Thank you all so much for any help you can provide! 非常感谢您提供的任何帮助! <3 <3 <3 <3 <3 <3

See the documentation : 请参阅文档

Cheerio is not a web browser Cheerio不是网络浏览器

Cheerio parses markup and provides an API for traversing/manipulating the resulting data structure. Cheerio解析标记并提供用于遍历/操纵结果数据结构的API。 It does not interpret the result as a web browser does. 它不像Web浏览器那样解释结果。 Specifically, it does not produce a visual rendering, apply CSS, load external resources, or execute JavaScript. 具体来说,它不会产生视觉效果,不会应用CSS,加载外部资源或执行JavaScript。 If your use case requires any of this functionality, you should consider projects like PhantomJS or JSDom. 如果您的用例需要任何此功能,则应考虑使用PhantomJS或JSm之类的项目。

Cheerio is a HTML parser. Cheerio是一个HTML解析器。

Cheerio can be used to select and manipulate dom elements, but it is not a full browser. Cheerio可用于选择和操作dom元素,但它不是完整的浏览器。

Cheerio only has access to the original source dom, which means that if the dom of a webpage is manipulated by javascript, Cheerio will not notice that change. Cheerio仅有权访问原始源dom,这意味着如果网页的dom是由javascript操作的,Cheerio将不会注意到该更改。

Cheerio cannot be used to interact with dom elements (ala jQuery) because it does not similarly execute within a window (js window) Cheerio无法用于与dom元素进行交互(ala jQuery),因为它不会类似地在窗口(js窗口)中执行

As of this moment, if you need to manipulate or select against js-rendered html, your best option is puppeteer. 从这一刻开始,如果您需要对js渲染的html进行操作或选择,那么最好的选择是puppeteer。 This is likely to change though, 但这可能会改变,

HTH HTH

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

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