Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8539817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:27:26+00:00 2026-06-11T11:27:26+00:00

I am attempting to write a spider using CasperJS used to parse pages and

  • 0

I am attempting to write a spider using CasperJS used to parse pages and extract relevant links. The site in question features a hierarchical view of files and folders. I have written a recursive method to perform the navigation through the file structure, but after the first iteration of the method my array is returning null, even though function is running correctly. Any help identifying the problem would be really appreciated.

var processPage = function() {

    //Gather links
    var links = this.evaluate(function() { //links is the array being set to null
        var elements = document.querySelectorAll("a");
        return Array.prototype.map.call(elements, function(e) {
            //check link matches our white list
            var matchesWhitelist = false;

            var fileDescription = e.querySelector("span").innerHTML;
            console.log("span text:" + fileDescription);

            //begin checking
            if (fileDescription.indexOf('.mp3') != -1) matchesWhitelist = true;
            //if (fileDescription.indexOf('.wmv') != -1) .... etc

            //failing that is the link for a folder rather than a file
            var hrefLink = e.getAttribute("href");
            if (hrefLink.indexOf('folder-files') != -1) matchesWhitelist = true;

            if (matchesWhitelist) {
                console.log('match');
                console.log('Adding link: ' + hrefLink)
                return hrefLink;
            }
            else {
                console.log('no match');
            }
        });
    });

console.log("linkslength: " + links.length); // links will be null upon recursion

for (var i = 0; i < links.length; i++) {

        //check link matches our 'whitelist'
        this.thenOpen("https://TLD" + links[i]).then(function() {
            this.echo("New URL: " + this.getCurrentUrl());

            //check for files
            if (this.exists(".fileDownload")) {
                //extract link
            } else {
                //assume that this is a 'folder' link and send to be processed for more links
                casper.then(processPage); //continue recursion
            };
        });
    }

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:27:27+00:00Added an answer on June 11, 2026 at 11:27 am

    You seem to want a reduced set.

    This means you should use .filter instead of .map, and return a truthy value if you want to keep the link, and a falsey value if not.

    return Array.prototype.filter.call(elements, function(e) {
    
         //...
    
        if (matchesWhitelist) {
            console.log('match');
            console.log('Adding link: ' + hrefLink)
            return true;
        }
        else {
            console.log('no match');
        }
    });
    

    And if you don’t need the logging, you would just return matchesWhitelist.

    return Array.prototype.filter.call(elements, function(e) {
    
         //...
    
        return matchesWhitelist;
    });
    

    So get rid of the logging, and you can reduce your filter to this.

    return Array.prototype.filter.call(elements, function(e) {
    
        return e.getAttribute("href").indexOf('folder-files') != -1 ||
               e.querySelector("span").innerHTML.indexOf('.mp3') != -1;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been attempting to write a VBA Script that can parse out other
I'm attempting to write a very basic script using javascript. What I want to
I am attempting to write to Azure Table Storage without using the TableContext and
I am attempting to write this type of program as stated in the question.
I'm attempting to write a simple buffer overflow using C on Mac OS X
I am attempting to write a .dll using a library that comes with some
I'm attempting to write an application to extract properties and code from proprietary IDE
I'm attempting to write a parser in Haskell using Parsec. Currently I have a
I am attempting to write an app in Jython running on Pyramid (debating using
I am attempting to write a bittorrent client. In order to parse the file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.