0

The big picture is to use Python to create a .txt file, then call the CasperJS script. Once the CasperJS script has run and saved its file (in this case the .png), have python grab that file and move to its next task.

As outlined in this question, another stack overflow question. I am looking primarily at the answer offered by Artjom B. towards the bottom. He suggests having Python save a .txt with the variables I would need, in my case (date, url) example, (2015-10-15, www.google.com)

I think part of my problem is I don't know much about JSON but I figure I would need to start by making both scripts, and then work on integrating them.

My Casper script is as follows:

var casper = require('casper').create({
    pageSettings: {
        loadImages: true, // The WebPage instance used by Casper will
        loadPlugins: false // use these settings
    }
});

var utils = require('utils');
var x = require('casper').selectXPath;
var fs = require('fs');
var url = //url provided in txt file that casper reads;
var date = //date provided in text file

casper.start().viewport(1920, 1080);

casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X)');

casper.thenOpen(url); //using the variable which came from the text file

casper.wait(3000, function () {
    this.echo(this.getTitle());
    console.log("Got title, now rendering image.");
    this.capture('date.png'); //saving the file as the date, provided by the .txt file. 
    console.log("got image of home page :D")
});

casper.then(function () {
    casper.exit();
});

casper.run();

I think that pretty much sums it up. Any direction on this is much appreciated, and any other links that you know of that can help me later down the road in running the Casper script inside a larger python script would be very helpful.

TLDR: How do I get CasperJS to read a text file, store the two values (date and url) as variables, and then use those variables to complete its job?

Community
  • 1
  • 1
Mxracer888
  • 341
  • 1
  • 4
  • 14

1 Answers1

0

JSON is very similar to plain JavaScript objects. Here is one:

{
    "url": "http://example.com/path",
    "date": "2015-10-15"
}

You can directly require JSON in CasperJS (PhantomJS) like this:

var config = require("config.json"); // file written from Python

And now you can use config.date and config.url in your script. You don't need assign those properties to dedicated variables, but of course you can do that to make the variables a little shorter:

var url = config.url;
var date = config.url;
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • Oh hey! I referenced your other post :) so you write: var config = require("config.json"); // file written from Python Do I replace the //file written from Python with the path to that file? i.e. C:\Users\(my comp username)\PycharmProjects\pythontextfile.txt Or am I misunderstanding that? – Mxracer888 Oct 09 '15 at 18:05
  • No, you replace `config.json` with the path. I don't know how full paths behave. Perhaps you want to use a relative path. – Artjom B. Oct 09 '15 at 18:07