Javascript CasperJS-DOM方法don';t在evaluate()函数中执行
这是代码中有问题的部分:Javascript CasperJS-DOM方法don';t在evaluate()函数中执行,javascript,web-scraping,phantomjs,casperjs,Javascript,Web Scraping,Phantomjs,Casperjs,这是代码中有问题的部分: var obj = {}; function putInObject() { obj.title = document.querySelector('[itemprop="title"]').innerText; obj.description = document.querySelector('[itemprop="description"]').innerText; } casper.then(function(){ casper
var obj = {};
function putInObject() {
obj.title = document.querySelector('[itemprop="title"]').innerText;
obj.description = document.querySelector('[itemprop="description"]').innerText;
}
casper.then(function(){
casper.wait(1000,function(){
links = this.evaluate(getItemLinks);
casper.each(links, function(self, link) {
self.thenOpen(link, function() {
this.echo(this.getTitle());
this.wait(7000, function(){
console.log("**************** \n WebPage is loaded \n ****************");
// Appearantly, The putInObject() doesn't run and "obj" remains null
casper.evaluate(putInObject);
this.echo(obj);
});
});
});
});
});
putInObject()
函数没有运行,甚至我替换了casper.evaluate(putInObject)代码>使用此选项:
this.evaluate(function() {
obj.title = document.querySelector('[itemprop="title"]').innerText;
obj.description = document.querySelector('[itemprop="description"]').innerText;
});
但它也不起作用,obj像[object]
一样被打印到控制台上
this.echo(this.getTitle())但是,代码>工作正常,并在控制台中打印页面标题
这是上述代码的日志:
[debug] [phantom] opening url: https://example.com/category/section/, HTTP GET
[debug] [phantom] Navigation requested: url=https://example.com/category/section/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://example.com/category/section/"
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=https://staticxx.facebook.com/connect/xd_arbiter/r/0F7S7QWJ0Ac.js?version=42#channel=f1413c20e7ccaa&origin=https%3A%2F%2Fexample.com, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 12/28 https://example.com/category/section/ (HTTP 200)
WebPage's Title: Section Name
[info] [phantom] Step anonymous 12/28: done in 81055ms.
[info] [phantom] Step _step 13/29 https://example.com/category/section/ (HTTP 200)
[info] [phantom] Step _step 13/29: done in 81075ms.
[info] [phantom] wait() finished waiting for 7000ms.
[info] [phantom] Step anonymous 14/30 https://example.com/category/section/ (HTTP 200)
****************
WebPage is loaded
****************
[object Object]
page.evaluate
就像一个通往另一个世界的门户,与我们的世界不同。那里可能有一个名为obj
的对象,但它与CasperJs脚本开头声明的obj
不同。这些是不同的物体。在page.evaluate
中发生的事情将保留在page.evaluate
-除非您特别要求返回某些数据
// This function will run inside of a web-page, not in the CasperJS script
function putInObject() {
var obj = {}; // UPDATED: here we also need to init this object
obj.title = document.querySelector('[itemprop="title"]').innerText;
obj.description = document.querySelector('[itemprop="description"]').innerText;
// return data from a web page context to CasperJS context
return obj;
}
...
// receive data in CasperJS context
obj = casper.evaluate(putInObject);
// If you want to view object's contents
console.log(JSON.stringify(obj));
谢谢你,瓦维洛夫。我确实听了你说的话并使用了你的提示,但是我从this.echo(obj)得到了null
代码>。这是修改后的代码:。关于console.log(JSON.stringify(obj))
,我假设应该在网页上下文中使用它,所以我在return
语句之前将它放在putInObject()
函数中,但我的终端(casperJS日志)中没有打印任何内容。我的错误,忘记了初始化网页中的obj
对象。请参阅更新的代码,下面是一个工作示例(尽管使用PhantomJS原始方言):