有没有办法使用capybara + phantomjs 来做到这一点.
或者更复杂的事情,比如将整页截图裁剪为特定的dom元素?
我正在尝试抓取此产品的尺寸:
问题:在选择产品颜色后加载尺寸.
在产品页面的源代码中,我可以看到下拉列表中有一个onchange-method:它单击了#postColor onchange形式.
选择下拉列表:
<select name="color" id="color" class="cposelect" onchange="document.getElementById('postColor').click();" style="width:150px;margin-right: 20px; float: left;">
<option selected="selected" onfocus="if (this.storeCurrentControl != null) storeCurrentControl(event, this);" value="0">Select colour</option>
<option onfocus="if (this.storeCurrentControl != null) storeCurrentControl(event, this);" value="-8027">Light Camel</option>
<option onfocus="if (this.storeCurrentControl != null) storeCurrentControl(event, this);" value="-9999">black</option>
</select>
Run Code Online (Sandbox Code Playgroud)
#postColor表单,单击onchange:
<input type="submit" name="postColor" value="" onclick="location.href=('./?model=10344-4180&color='+document.forms[0].color.value+'&size='+document.forms[0].size.value+'&addbread=OUTLET&addbread2=DRIZIA&currentimage='+document.getElementById('currentimage').value+'&selectedmi=a1_INDEX_14&prev=10850-4314&next=10413-4183'); return false;" id="postColor" class="cpobutton " style="display: none;">
Run Code Online (Sandbox Code Playgroud)
到目前为止这是我的代码,它不起作用:
casper.start('http://shop.baumundpferdgarten.com/showmodel/?model=10344-4180&addbread=OUTLET&addbread2=DRIZIA&color=0¤timage=1&selectedmi=a1_INDEX_14', function() {
this.test.assertExists('select[name="color"] option:nth-child(2)');
this.click('select[name="color"] option:nth-child(2)');
this.waitForSelector('select[name="size"] option:nth-child(2)', function() {
this.test.pass('selector is !');
var sizes = this.evaluate(function() {
console.log("======== evaluating ========");
// …Run Code Online (Sandbox Code Playgroud) 我正在尝试在tumblr中自动登录,运行此处提出的脚本,但phantomjs仅显示失败的尝试:
[info] [phantom] Starting...
[info] [phantom] Running suite: 3 steps
[debug] [phantom] opening url: https://tumblr.com/login, HTTP GET
[debug] [phantom] Navigation requested: url=https://tumblr.com/login, type=Other,willNavigate=true, isMainFrame=true
[warning] [phantom] Loading resource failed with status=fail: https://tumblr.com/login
[debug] [phantom] Successfully injected Casper client-side utilities
[debug] [phantom] Logging in
[info] [remote] attempting to fetch form element from selector: '#signup_form'
[info] [phantom] Step anonymous 2/3: done in 2899ms.
[info] [phantom] Step _step 3/3: done in 2912ms.
[warning] [phantom] Casper.waitFor() timeout
[error] [phantom] Wait …Run Code Online (Sandbox Code Playgroud) 我正试图到达一个Instagram页面,但没有运气.我一直收到错误和一个空白的截图.
错误文字:
TypeError: 'undefined' is not a function (evaluating 'a.createDescriptor.bind(null,t)')
Run Code Online (Sandbox Code Playgroud)
Casperjs --version是1.1.0-beta3.
基本上我使用以下代码:
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
pageSettings: {
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
},
loadPlugins: true
});
casper.on( 'page.error', function (msg, trace) {
this.echo( 'Error: ' + msg, 'ERROR' );
});
casper.start('http://instagram.com/hello', function() {
casper.wait(3000, function() {
this.capture('screen.png');
});
});
casper.run(function() {
this.exit();
});
Run Code Online (Sandbox Code Playgroud) 我认为我发送的每个请求都是在没有cookie的情况下发送的.
当我像这样听onResourceRequested事件时:
this.page.onResourceRequested = function(request) {
utils.dump(request);
};
Run Code Online (Sandbox Code Playgroud)
并且每个请求都具有相同形式的标头:
"headers": [
{
"name": "User-Agent",
"value": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
},
{
"name": "Accept",
"value": "text/css,*/*;q=0.1"
},
{
"name": "Referer",
"value": "https://some_site.com/page"
}
],
Run Code Online (Sandbox Code Playgroud)
我从来没有得到应该在那里的cookie的标题..
当我尝试查看cookie时 - 我运行这个:
utils.dump(this.page.cookies);
Run Code Online (Sandbox Code Playgroud)
我得到了许多cookie条目的列表.
我认为这给我的刮擦脚本带来了一些错误.
你的意见?谢谢.
编辑
我尝试发出POST请求来下载文件.我可以登录该站点,浏览到几个页面,进入下载页面 - 但是,当我发送请求时,我收到一条消息"错误:要注册该站点,您必须启用浏览器才能接受cookie. "
这就是为什么它令人困惑 - 我可以登录并浏览网站(所以我必须传递一些cookie)但我无法下载(所以我可能没有这里的cookie ..)
我在windows运行时遇到以下错误,spookyjs但代码正在运行linux.
events.js:85
throw er; // Unhandled 'error' event
^
Error: spawn casperjs ENOENT
at exports._errnoException (util.js:746:11
at Process.ChildProcess._handle.onexit (ch
at child_process.js:1137:20
at process._tickCallback (node.js:355:11)
Run Code Online (Sandbox Code Playgroud)
我已经安装了所有依赖项并添加了PATHvaraiables
npm install -g casperjs在命令提示符casperjs下工作
安装npm作为全局使用
npm install -g npm
Run Code Online (Sandbox Code Playgroud)卸载0.12并降级为0.10.
添加了System变量C:\Windows\System32;.
任何想法或建议都将不胜感激
我是PhantomJS/CasperJS的初学者.
我只是想开始一个会话并验证它没问题.
这是我的代码:
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
pageSettings: {
loadImages: false,
loadPlugins: false,
userAgent: 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0'
}
});
casper.on('remote.message', function(msg) {
this.echo('remote message caught: ' + msg);
});
casper.on("page.error", function(msg,trace) {
this.echo("Page Error: " + msg, "ERROR");
});
casper.start('https://www.google.fr');
casper.then(function(){
console.log('Page Loaded');
this.test.assertTitle('Google', 'Welcome to Google');
});
casper.run();
Run Code Online (Sandbox Code Playgroud)
当我运行这个简单的脚本时,我得到:
C:\Users\Booky\Documents\nike_project>casperjs --ignore-ssl-errors=true --ssl-protocol=tlsv1 debug.js
C:\Users\Booky\Documents\nike_project>[info] [phantom] Starting...
[info] [phantom] Running suite: 3 steps
[debug] [phantom] opening url: …Run Code Online (Sandbox Code Playgroud) 我想让我的casper登录并在会话期间保持登录状态.可能吗?如果是这样,怎么样?
为了调试purporses,我需要查看整个请求:标头和数据.我怎样才能做到这一点?
当我运行casperjs脚本时,我的脚本在控制台消息上被阻止[info] [幻影]开始...我不明白为什么.
我的剧本
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
});
var login = '******@hotmail.fr';
var password = '*******';
var baseURL = 'http://fr.bazarchic.com/';
casper.start('http://fr.bazarchic.com/', function(){
this.fill('form[action="/login/"', {
'email' : login,
'pass' : password
}, true)
});
casper.then(function() {
var pageTitle = this.evaluate(function(){
return document.title;
})
this.echo(pageTitle);
});
Run Code Online (Sandbox Code Playgroud)