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

  • Home
  • SEARCH
  • 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 8995373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:36:41+00:00 2026-06-15T23:36:41+00:00

pjscrape is great for scraping, however I’m having a hard time figuring out how

  • 0

pjscrape is great for scraping, however I’m having a hard time figuring out how to pass in arguments to my scraper.

It takes command line arguments for config files but I’m not sure what the scope of my config functions/variables is.

I would love to have configs per domain which includes base URLs, selectors etc. and build a somewhat generic scraper that would be able to read from this config.

How can I do that?

  • 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-15T23:36:42+00:00Added an answer on June 15, 2026 at 11:36 pm

    Pjscrape will evaluate all arguments as config files in the global scope, and you can pass in as many config files as you want. So configuring the per-domain scrapers in one or multiple files should be straightforward. For example:

    base_config.js

    var myConfigs = [];
    

    my_site.js

    myConfigs.push({
        title: 'My Site',
        url: 'http://www.example.com',
        scraper: 'div#my-site-info'
    });
    

    scraper.js

    myConfigs.forEach(function(config) {
        pjs.addSuite({
            title: config.title,
            url: config.url,
            scraper: config.scraper
        })
    });
    

    Then invoke like:

    ~> phantomjs /path/to/pjscrape.js base_config.js my_site.js <...> scraper.js
    

    The tricky part here is when you want to use scraper functions, not just selectors. PhantomJS runs the scrapers in a “sandboxed” environment, which does not have access to your global scope variables. So this will not work:

    myConfigs.forEach(function(config) {
        pjs.addSuite({
            scraper: function() {
                // will fail - config is undefined!
                return $(config.selector).text();
            }
        })
    });
    

    This is a trivial example, but you get the idea. PhantomJS now has native support for passing in arguments to page.evaluate, but these aren’t built into Pjscrape yet. There are basically two ways to deal with this:

    • Always deal with functions that don’t need access to the outer scope. So each site config file would specify full scraper functions, not just pluggable variables.

    • Create your scrapers with new Function("..."), passing in your variables as you create the string. This is how Pjscrape does it under the hood, but fair warning – it can get ugly quickly in all but the most straightforward cases. One method I’ve used here is to use Function#toString and pass in arguments. This might look like this:

      function makeFunc(f, args) {
          return new Function('return (' + f.toString() + ')' +
              '.apply(null, ' + JSON.stringify(args) + ');');
      }
      
      myConfigs.forEach(function(config) {
          pjs.addSuite({
              scraper: makeFunc(function(selector) {
                  // works - config.selector is passed in as the "selector" arg
                  return $(selector).text();
              }, [config.selector])
          })
      });
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Overview I am trying to create a very basic scraper with PhantomJS and pjscrape
trying to scrape multiple pages but can't get the urlid array to work within

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.