简体   繁体   English

如何在NodeJS中使用PhantomJS模拟鼠标悬停或在页面上运行JS函数

[英]How to emulate mouseover or run JS function on page with PhantomJS in NodeJS

NodeJS, PhantomJS, content parsing with Cheerio NodeJS,PhantomJS,使用Cheerio进行内容解析

Need to parse webpage, that contains dynamically loaded div(hint). 需要解析包含动态加载的div(hint)的网页。 The event can be on many table td's, here is an example 该事件可以在许多表td上发生,这是一个示例

在此处输入图片说明

When I 'mouseover' on specific td I see this orange block with data, it's dynamically loaded with function, like this 当我在特定的td上“鼠标悬停”时,我看到这个带有数据的橙色块,它会动态加载函数,就像这样

onmouseover="page.hist(this,'P-0.00-0-0','355svxv498x0x0',417,event,0,1)"

I can view this info only after the page is loaded. 我只能在页面加载后查看此信息。 Need to a specific row, only Marathonbet. 需要特定的一行,只有Marathonbet。

When the function runs, the text is loaded into another div (id='tooltip') and shown to the user. 函数运行时,文本将被加载到另一个div(id ='tooltip')中并显示给用户。

I use phantom to parse the content of this page, everything OK with static values, but how I can receive this dynamically generated block to my rendered web page inside node router? 我使用phantom解析此页面的内容,使用静态值可以正常运行,但是如何将动态生成的块接收到节点路由器内部渲染的网页上呢? I see 2 ways: 我看到2种方法:

  1. Emulate mouse move on this coordinates to show needed text, but there is a problem, how I can known it's coords? 在此坐标上模拟鼠标移动以显示所需的文本,但是有一个问题,我怎么知道它的坐标?
  2. Emulate function start after page is loaded and i known they codes ('355svxv498x0x0',417), but how I can run this function from node, from phantom? 模拟函数在页面加载后启动,并且我知道它们编码('355svxv498x0x0',417),但是如何从节点,幻像中运行此函数呢?

    Here is some code, that recieve static page content in my router 这是一些代码,可以在路由器中接收静态页面内容

``` ```

phantom.create(config.phantomParams).then(ph => {
    _ph = ph;
    return _ph.createPage();
}).then(page => {
    _page = page;
    return _page.on('onConsoleMessage', function (msg) {
        console.log(msg);
    });
}).then(() => {
    return _page.on('viewportSize', {width: 1920, height: 1080});
}).then(() => {
    return _page.on('dpi', 130)
}).then(() => {
    _page.setting('userAgent', config.userAgent);
    return _page.open(matchLink);
}).then(() => {
    return _page.property('content');
}).then(content => {
    let $ = cheerio.load(content);

    // working with content and get needed elements

    console.log($.html());
}).then(() => {
    _page.close();
    _ph.exit();
});

``` Should I use Casper/Spooky, or anyone can explain how to use it in this case? 我应该使用Casper / Spooky,还是有人可以解释这种情况下的使用方法?

UPD. UPD。 Trying with puppeteer, the code 尝试使用puppeteer,代码

``` ```

let matchLink = 'http://www.oddsportal.com/soccer/world/club-friendly/san-carlos-guadalupe-xnsUg7zB/';

(async () => {
    const browser = await puppeteer.launch({
        args: [
            '--proxy-server=46.101.167.43:80',
        ]});
    const page = await browser.newPage();
    await browser.userAgent(config.userAgent);
    await page.setViewport({width: 1440, height: 960});
    await page.goto(matchLink);
    await page.evaluate(() => page.hist(this,'P-0.00-0-0','355svxv464x0x7omg7',381,event,0,1));

    let bodyHTML = await page.evaluate(() => document.body.innerHTML);

    console.log(bodyHTML);
    await page.screenshot({path: 'example.png'});

    await browser.close();
})();

``` ```

Get ``` 得到`

(node:8591) UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: Cannot read property 'stopPropagation' of undefined
    at toolTip (http://www.oddsportal.com/res/x/global-180713073352.js:1:145511)
    at TableSet.historyTooltip (http://www.oddsportal.com/res/x/global-180713073352.js:1:631115)
    at PageEvent.PagePrototype.hist (http://www.oddsportal.com/res/x/global-180713073352.js:1:487314)
    at __puppeteer_evaluation_script__:1:13
    at ExecutionContext.evaluateHandle (/home/gil/Projects/oddsbot/node_modules/puppeteer/lib/ExecutionContext.js:97:13)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

``` ```

Error in target JS file, may be something with request.. 目标JS文件中的错误,可能与请求有关。

Since you're open to suggestions I propose puppeteer It's a native node.js module that opens pages in the newest Chromium (especially useful since PhantomJS is very outdated) and is close to PhantomJS in terms of doing thinkgs. 由于您愿意接受建议,因此我建议使用puppeteer。它是一个本机的node.js模块,可以在最新的Chromium中打开页面(由于PhantomJS非常过时,因此特别有用),并且在进行思考方面与PhantomJS十分接近。

If you also use node.js 8.x, async/await syntax is available for working with promises and it makes scraping with puppeteer a breeze. 如果您还使用node.js 8.x,则可以使用async / await语法来处理Promise,这使得轻轻松松地进行伪装就很容易了。

So to run that function in puppeteer you would run 因此,要在puppeteer中运行该功能,您需要运行

await page.evaluate(() => page.hist(this,'P-0.00-0-0','355svxv498x0x0',417,event,0,1) );

Update 更新资料

Puppeteer has lots of convenience helpers, one of them is page.hover that literally will hover a pointer over an element: Puppeteer有很多便利帮助器,其中之一是page.hover ,它实际上会将指针悬停在元素上:

await page.hover('td.some_selector');

But should you want to continue using Phantomjs and the excellent phantom module, you can: 但是,如果您想继续使用Phantomjs和出色的phantom模块,则可以:

_page.evaluate(function() {
    page.hist(this,'P-0.00-0-0','355svxv498x0x0',417,event,0,1)
})

Documents on page.evaluate : http://phantomjs.org/api/webpage/method/evaluate.html page.evaluate上的page.evaluatehttp : page.evaluate

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

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