0

I'm new to programing in languages more suited to the web, but I have programmed in vba for excel.

What I would like to do is:

  1. pass a list (in python) to a casper.js script.
  2. Inside the casperjs script I would like to iterate over the python object (a list of search terms)
  3. In the casper script I would like to query google for search terms
  4. Once queried I would like to store the results of these queries in an array, which I concatenate together while iterating over the python object.
  5. Then once I have searched for all the search-terms and found results I would like to return the RESULTS array to python, so I can further manipulate the data.

QUESTION --> I'm not sure how to write the python function to pass an object to casper.

QUESTION --> I'm also not sure how to write the casper function to pass an javascript object back to python.

Here is my python code.

import os
import subprocess
scriptType = 'casperScript.js'
APP_ROOT = os.path.dirname(os.path.realpath(__file__))
PHANTOM = '\casperjs\bin\casperjs'
SCRIPT = os.path.join(APP_ROOT, test.js)
params = [PHANTOM, SCRIPT]
subprocess.check_output(params)

js CODE

var casper = require('casper').create();
casper.start('http://google.com/', function() {
this.echo(this.getTitle());
});
casper.run();
yoshiserry
  • 20,175
  • 35
  • 77
  • 104
  • You wanted that I help you with this question from [here](http://stackoverflow.com/questions/23761795/injecting-javacsript-code-into-an-on-click-event-with-javascript-and-casper-js#comment37587032_23761795), but still haven't responded. Did it help or not? What's the problem? – Artjom B. Jul 01 '14 at 00:53

2 Answers2

0

Could you use JSON to send the data to the script and then decode it when you get it back?

Python:

json = json.dumps(stuff) //Turn object into string to pass to js

Load a json file into python:

with open(location + '/module_status.json') as data_file:
    data = json.load(data_file);

Deserialize a json string to an object in python

Javascript:

arr = JSON.parse(json) //Turn a json string to a js array json = JSON.stringify(arr) //Turn an array to a json string ready to send to python

Community
  • 1
  • 1
woverton
  • 441
  • 1
  • 3
  • 12
  • is STUFF the list object I had with search terms in it? Once you pass stuff to the json object JSON and then create a Javascript Array object, how do you pass the array object into the casperJS script? In ecvel vba it would be sub CasperScript(SomeParameter) then for i in SomeParameter.length ....(do javascript stuff here) – yoshiserry May 23 '14 at 11:22
  • Stuff is your list/object yes. Can you call your casperjs script with command args? – woverton May 23 '14 at 11:25
  • At the moment I just go into power shell and type casperjs python script name.py I don't know how to call the python script while passing in arguments and I don't know how to pass arguments like a search string or list to casperjs script either. I'm new. Sorry – yoshiserry May 23 '14 at 12:48
  • You can load json from a file with python (I added this to my answer). And you can pass it in as an argument to your js script and catch it with the CLI http://docs.casperjs.org/en/latest/cli.html – woverton May 23 '14 at 13:33
  • @yoshiserry Basically what you can do is serialize your list/object in python into JSON and write the JSON into a file. Then invoke your casperjs script from python which reads this file (can be hardcoded and using `fs` module), parses the JSON, executes something, stringify the results, write the results into a known file. The python script continues execution by reading the result file, parsing it from JSON and doing stuff. – Artjom B. May 23 '14 at 15:02
  • Does this mean for a python variable to be parsed to casper it must first be writter to a file, to be then turned into a json object with json.load? I.e. it can't just be parsed as a straight parameter with call results(capser_script.js, some_list[]). Could you give a trivial example of getting the now json file into casper js iterating over it and passing it back. with fs please if it can't be done as I described in this comment, by passing the object straight to a function which has the casper script as one parameter and the object I want to iterate over as another parameter. – yoshiserry May 25 '14 at 21:53
0

You can use two temporary files, one for input and the other for output of the casperjs script. woverton's answer is ok, but lacks a little detail. It is better to explicitly dump your JSON into a file than trying to parse the console messages from casperjs as they can be interleaved with debug strings and such.

In python:

import tempfile
import json
import os
import subprocess

APP_ROOT = os.path.dirname(os.path.realpath(__file__))
PHANTOM = '\casperjs\bin\casperjs'
SCRIPT = os.path.join(APP_ROOT, test.js)

input = tempfile.NamedTemporaryFile(mode="w", delete=False)
output = tempfile.NamedTemporaryFile(mode="r", delete=False)

yourObj = {"someKey": "someData"}
yourJSON = json.dumps(yourObj)
input.file.write(yourJSON)
# you need to close the temporary input and output file because casperjs does operations on them
input.file.close()
input = None
output.file.close()

print "yourJSON", yourJSON

# pass only file names
params = [PHANTOM, SCRIPT, input.name, output.name]
subprocess.check_output(params)

# you need to open the temporary output file again
output = open(output.name, "r")
yourReturnedJSON = json.load(output)
print "returned", yourReturnedJSON
output.close()
output = None

At the end, the temporary files will be automatically deleted when the objects are garbage collected.

In casperjs:

var casper = require('casper').create();
var fs = require("fs");

var input = casper.cli.raw.get(0);
var output = casper.cli.raw.get(1);
input = JSON.parse(fs.read(input));
input.casper = "done"; // add another property
input = JSON.stringify(input);
fs.write(output, input, "w"); // input written to output
casper.exit();

The casperjs script isn't doing anything useful. It just writes the inputfile to the output file with an added property.

Community
  • 1
  • 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222