Giter Site home page Giter Site logo

Using Asynchronous ScraperFn about scraperjs HOT 7 CLOSED

ruipgil avatar ruipgil commented on August 15, 2024
Using Asynchronous ScraperFn

from scraperjs.

Comments (7)

ruipgil avatar ruipgil commented on August 15, 2024

Could you provide an example?

from scraperjs.

ksloan avatar ksloan commented on August 15, 2024

For example on this page there is a 'load more' button to get all the events to show in the DOM before I can scrape them. So I've set up an interval to check the status of the load more button, and click it if needed like so

if ($('.load_more_link').length > 0) {

    var clicker = setInterval(function() {
        $('.load_more_link').click()

        if ($('.load_more_link').length == 0) {
            clearInterval(clicker) // done here
        };

    }, 1000)

}

Then once it's done, I want to return the total number of events along with some other info... but I don't see how I can return any info once the function becomes asynchronous.

from scraperjs.

cjackie avatar cjackie commented on August 15, 2024

This is a common pattern.
phantomjs does have the functionality to trigger events after a page has been loaded. Maybe we can add it into ScraperPromise.js? so that we have something like:

var scraperjs = require('scraperjs');
scraperjs.DynamicScraper.create('https://news.ycombinator.com/')
             .triggerEvent(function(){
                 //trigger events
              });

But, since phantomjs is sandboxed, we need to think of a way to tell the promise that event has finished. set a waiting time is one way (poor one).

from scraperjs.

ruipgil avatar ruipgil commented on August 15, 2024

@ksloan , for that kind of sites I find that it is better just to inspect the ajax calls and work from there. You could also try to to use the delay promise between two scrape promises.
@cjackie , I might look into that sometime in the future.

from scraperjs.

ruipgil avatar ruipgil commented on August 15, 2024

The v0.3.0 has an async promise, you can see the promise which allows to check for events and then trigger them.

from scraperjs.

ksloan avatar ksloan commented on August 15, 2024

Amazing!! Thank you!

from scraperjs.

emgould avatar emgould commented on August 15, 2024

Can you provide example using aync?

from scraperjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.