PhantomJS executing JavaScript in a popup for data extraction

大憨熊 提交于 2019-12-11 08:55:21

问题


So I have a web page with some photos of people. When you click on a photo of the person the JavaScript is executed and produces a popup with some more detailed information such as a description etc.

The link for each photo is as follows:

<a href="javascript:void(0)" data="10019"  class="seeMore"></a>

First I want to start with the basics, of just extracting the description etc. from one single person. So I want to execute the JavaScript above to write the popup window, and when I'm on the popup window I can then extract the content of the div's on the popup.

I've looked at PhantomJS and I really don't know where to start. I've used Cheerio to get some simple information from the page, and I want to move on to executing the popup window through JS and then extracting data from that.

Any help would be appreciated as I'm a bit of a newbie to screen scraping in general.


回答1:


You can do this analogous to how CasperJS does it.

page.open(yourUrl, function(success){
    // mainPage is loaded, so every next page could be a popup
    page.onPageCreated = function onPageCreated(popupPage) {
        popupPage.onLoadFinished = function onLoadFinished() {
            popupPage.evaluate(function(){
                // do something in popup page context like extracting data
            });
        };
    };

    // click to trigger the popup
    page.evaluate(function(){
        document.querySelector("a.seeMore").click();
        // or something from here: http://stackoverflow.com/questions/15739263/phantomjs-click-an-element
    });
});

Do not forget to overwrite/nullify page.onPageCreated before navigating away from the main page.



来源:https://stackoverflow.com/questions/25852126/phantomjs-executing-javascript-in-a-popup-for-data-extraction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!