将python对象传递到casperjs脚本中,对该对象进行迭代并将结果对象返回给python [英] passing a python object into a casperjs script iterating over the object and returning a result object to python

查看:77
本文介绍了将python对象传递到casperjs脚本中,对该对象进行迭代并将结果对象返回给python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次使用更适合网络的语言进行编程,但是我已经在vba中为excel编程.

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

我想做的是:

  1. 将列表(在python中)传递给casper.js脚本.
  2. 在casperjs脚本中,我想遍历python对象(搜索项列表)
  3. 在Casper脚本中,我想查询搜索条件的Google
  4. 一旦查询,我想将这些查询的结果存储在一个数组中,在遍历python对象时,它们会串联在一起.
  5. 然后,一旦我搜索了所有搜索词并找到了结果,我想将RESULTS数组返回给python,以便我可以进一步处理数据.

问题->我不确定如何编写将对象传递给casper的python函数.

问题->我也不确定如何编写casper函数以将javascript对象传递回python.

这是我的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代码

var casper = require('casper').create();
casper.start('http://google.com/', function() {
this.echo(this.getTitle());
});
casper.run();

推荐答案

您可以使用两个临时文件,一个用于输入,另一个用于输出casperjs脚本. woverton的答案可以,但是缺少一些细节.最好将JSON明确转储到文件中,而不是尝试从casperjs中解析控制台消息,因为它们可以与调试字符串等交织在一起.

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.

在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.

在casperjs中:

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();

casperjs脚本没有做任何有用的事情.它只是将输入文件添加到具有附加属性的输出文件中.

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

这篇关于将python对象传递到casperjs脚本中,对该对象进行迭代并将结果对象返回给python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆