这是一个关于如何将cookie从一个casperjs页面持久存储到另一个页面的问题.
所以基本上我得到一个nodejs文件,它生成casperjs作为工作人员来完成某些任务..一个是登录,一旦登录我将cookie存储在一个文件中.
当我产生下一个casper worker时..我希望它使用cookie而不必再次登录..这两种方法都失败了:
第一:当我产生工人capserjs时,我添加了--cookies-file =./ cookiefilename ie
var child = spawn('casperjs',['scrape.js','--cookies-file=./'+cookieFileName]);
第二:在casperjs工作文件中..我让它读取并从文件中设置cookie即
var casper = require('casper').create();
var cookieFileName = 'monsterCookie.txt';
// grab cookies from file
var fs = require('fs');
var utils = require('utils');
var cookies = fs.read(cookieFileName);
casper.page.setCookies(cookies);
casper.start('domain/page.html', function() {
//FAIL! cookies aren't used here
this.debugHTML();
});
casper.run();
Run Code Online (Sandbox Code Playgroud)
笔记:
start从页面删除cookies吗?如果是这样我该怎么做呢?[一般]饼干="@变体(\ 0\0\0\0x7F部分\ 0\0\0\x16QList\0\0\0\0\X1\0\0\0 \n\0\0\0YCNTR = LB; expires = Tue,09-Apr-2013 17:12:05 GMT; domain = …
可以在CasperJS脚本中读/写文件吗?
var fs = require('fs');
var data = fs.readFileSync('testdata.data', 'utf-8');
console.log(data);
Run Code Online (Sandbox Code Playgroud)
致电casperjs fileio.js回报:
'undefined' is not a function
Run Code Online (Sandbox Code Playgroud)
即使跑完了之后npm install fs.
如果没有加分点:解释原因?
我可以在PhantomJS和/或CasperJS上使用交互式JS调试器吗?
intellij-idea javascript-debugger webstorm phantomjs casperjs
我已经下载了最新版本的casperjs(1.03)和phantomjs(1.9.2).
所以我从casper页面中获取了这个简单的小脚本:
var casper = require('casper').create({
verbose: true,
logLevel: "debug"
});
var casper = new require('casper').Casper();
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,我收到以下错误:
noname:phantomjs-1.9.2 Tom$ casperjs/bin/casperjs tipico2.js
Fatal: [Errno 2] No such file or directory; did you install phantomjs?
Run Code Online (Sandbox Code Playgroud)
所以,这是我的目录结构:
phantomjs-1.9.2/ <-- the folder containing phantomjs
phantomjs-1.9.2/casperjs/ <-- a subfolder containting casperjs
Run Code Online (Sandbox Code Playgroud)
为什么不起作用?
有没有办法一致地检测PhantomJS/CasperJS?我一直在处理一堆用它构建的恶意垃圾邮件,并且已经能够基于某些行为阻止它们,但我很好奇是否有一种坚如磐石的方式来了解CasperJS是否正在使用中,如同处理不断适应变得有点烦人.
我不相信使用Captchas.他们是负面的用户体验,ReCaptcha从未在我的MediaWiki安装上阻止垃圾邮件.由于我们的网站没有用户注册(匿名讨论板),我们需要为每个帖子都有一个Captcha条目.我们每天会收到数千个合法的帖子,而且Captcha会看到这个号码.
我知道有一些网页PhantomJS/CasperJS无法打开,我想知道这个是否是其中之一:https://maizepages.umich.edu .CasperJS给出错误:PhantomJS无法打开页面状态=失败.
我尝试忽略-ssl-errors并更改我的用户代理但我不确定如何确定使用哪些.
我现在正在做的就是基本的casper设置在casper.start(url, function () { ... })哪里url=https://maizepages.umich.edu;
打开网页并截取屏幕截图.
只使用phantomjs:(这是一个简单的脚本,实际上它是在他们的文档中使用的示例脚本.http://phantomjs.org/screen-capture.html
var page = require('webpage').create();
page.open('http://github.com/', function() {
page.render('github.png');
phantom.exit();
});
Run Code Online (Sandbox Code Playgroud)
问题是,对于一些网站(如github),足够有趣的是以某种方式检测并且不提供phantomjs并且没有任何东西被渲染.结果是github.png一个空白的白色png文件.
用说:"google.com"替换github,你会得到一个很好的(正确的)截图.
起初我认为这是一个Phantomjs问题所以我尝试通过Casperjs运行它:
casper.start('http://www.github.com/', function() {
this.captureSelector('github.png', 'body');
});
casper.run();
Run Code Online (Sandbox Code Playgroud)
但我得到与Phantomjs相同的行为.
所以我认为这很可能是用户代理问题.如:Github嗅出Phantomjs并决定不显示页面.所以我设置用户代理如下,但仍然无法正常工作.
var page = require('webpage').create();
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36';
page.open('http://github.com/', function() {
page.render('github.png');
phantom.exit();
});
Run Code Online (Sandbox Code Playgroud)
所以我试图解析页面,显然有些网站(再次像github)似乎没有发送任何网络.
使用casperjs我试图打印标题.对于google.com我回来了Google但是对于github.com我回来了bupkis.示例代码:
var casper = require('casper').create();
casper.start('http://github.com/', function() {
this.echo(this.getTitle());
});
casper.run();
Run Code Online (Sandbox Code Playgroud)
与上述相同也在纯粹的幻影中产生相同的结果.
这可能是一个时间问题吗?github只是超级慢吗?我怀疑它,但无论如何都要测试..
var page = require('webpage').create();
page.open('http://github.com', function (status) {
/* irrelevant */ …Run Code Online (Sandbox Code Playgroud) 我有简单的页面用javascript验证输入中写的电子邮件:
email.html:
<!DOCTYPE html>
<html>
<head>
<title>Email validation</title>
<script src="email.js"></script>
</head>
<body>
<span style="padding: 5px;">
<input type="text" id="email-input" placeholder="Email..."></input>
</span>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
email.js:
var checkEmail = function() {
var regexp = /BIG_REGEX/;
var email = document.getElementById('email-input').value;
if (email === '')
removeFrame();
else if (regexp.test(email))
drawFrame('green');
else
drawFrame('red');
};
var removeFrame = function() {
var input = document.getElementById('email-input');
input.parentNode.style.backgroundColor = input.parentNode.parentNode.style.backgroundColor;
};
var drawFrame = function(color) {
var input = document.getElementById('email-input');
input.parentNode.style.backgroundColor = color;
};
window.onload = function() {
document.getElementById('email-input').onkeyup = …Run Code Online (Sandbox Code Playgroud) 我有输入元素如图所示:
<input type="text" class="bg-white" id="couponCode" value="">
如何使用casperJs设置/填充其值
我正在尝试抓取一个网页,其中包含许多下拉列表,并且表单中的值是相互依赖的.在许多方面,我需要代码等待页面刷新完成.例如,从列表中选择一个选项后,代码应该等到根据此选择填充下一个列表.如果有人可以指点,那将是非常有帮助的,因为奇怪的是我的代码只有在我提供了如此多的不必要的日志记录语句之后才能工作,这反过来又造成了一些延迟.任何改进代码的建议都会非常有用.
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
userAgent: 'Mozilla/5.0 poi poi poi (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22',
pageSettings: {}
});
casper.start('http://www.abc.com', function () {
console.log("casper started");
this.fill('form[action="http://www.abc.com/forum/member.php"]', {
quick_username: "qwe",
quick_password: "qwe"
}, true);
this.capture('screen.png');
});
casper.thenOpen("http://www.abc.com/search/index.php").then(function () {
this.click('input[type="checkbox"][name="firstparam"]');
this.click('a#poi');
casper.evaluate(function () {
document.getElementsByName("status")[0].value = 1;
document.getElementsByName("state")[0].value = 1078;
changeState(); //This function is associated with the dropdown ie state
and the page reloads at this point. Only after complete refresh …Run Code Online (Sandbox Code Playgroud)