简体   繁体   中英

How to properly close phantomjs webpage after use?

I'm trying to read some data from 200+ web pages with PhantomJS and typescript/rxjs What I came up so far is this

Observable.fromPromise(phantom.createPage()).flatMap(jobPage => {
            return Observable.fromPromise(jobPage.open(url)).flatMap(status => {
                if (status !== "success") {
                    console.error("Couldn't load job page for url " + url + " Status: " + status);
                    jobPage.close();
                    return Observable.of(undefined)
                } else {
                    return Observable.fromPromise(jobPage.evaluate(function () {
                        //do some content reading, return data

                        return data;
                    }));
                }
            });
        })

And it works, but with every page it gets slower and slower, and finally ends with Memory Exhausted message from Phantom. I guess it's because I do not close the web pages I'm creating, but I dont have any idea how to do it such case (flatMap creates a new one, I need it for extraction later, and Observable.fromPromise() does not allow me to close the page after I'm done.

Any help is appreciated

Ok, figured it out, just need to use

Observable.fromPromise(phantom.createPage()).flatMap(jobPage => {
            //stuff as before
        }).finally(function(){
                        jobPage.close();
                    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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