Javascript 使用phantomjs从google获取股票报价

Javascript 使用phantomjs从google获取股票报价,javascript,jquery,web-scraping,phantomjs,Javascript,Jquery,Web Scraping,Phantomjs,我正试图在span.\u Rnb.fmob\u pr.fac-l中获取文本。我相信这是股票报价的选择。现在它只返回控制台opensuccess跳过一行或两行,然后退出 var webPage = require("webpage"); var page = webPage.create(); var url = "https://www.google.com/search?q=goog+stock" page.open(url, function(status){ console.lo

我正试图在
span.\u Rnb.fmob\u pr.fac-l
中获取文本。我相信这是股票报价的选择。现在它只返回控制台
opensuccess
跳过一行或两行,然后退出

var webPage = require("webpage");
var page = webPage.create();
var url = "https://www.google.com/search?q=goog+stock"

page.open(url, function(status){
    console.log("open", status)
    page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function(status){
        // console.log($)
        var price = page.evaluate(function(){
            return $("span._Rnb").text()
            // return $(".kno-rdesc").find("span").text()
             // return  $("._Rnb").text();  
            // return  document.title       
            // return $(document).find("title").text()
        })
        console.log(price)
        phantom.exit()
    })
})
我不知道如何调试这个。我做了
console.log($)
,但什么也没有得到


我能够在页面上获得h3链接标签。我想知道为什么我无法获得股票报价。

页面。evaluate
是一个异步函数。这意味着它会在函数中的内容实际发生之前立即返回。这是合乎逻辑的,因为页面渲染需要时间。 但是,您的
console.log(price)
和更重要的
phantom.exit()
会立即发生

最简单的解决方案是将
控制台.log
幻影.exit
设置为长超时(5-10秒),以使页面正确计算


更合适的解决方案是某种轮询,甚至事件从评估页面内部传递到外部的phantom.js代码-请随意尝试。

选择器
返回$((表:第n个类型(1)tbody>tr>td>span>b)).eq(0).text()
有效

page.open(url, function(status){
    console.log("open", status)
    page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function(status){
         var price = page.evaluate(function(){
             return $("table:nth-of-type(1) tbody > tr > td > span > b").eq(0).text()
         })
         console.log(price)
         phantom.exit()
     })
})

否,
$(“span.\Rnb”).text()是同步的,因此整个
页面.evaluate
调用将是同步的。您使用哪个PhantomJS版本?请注册到
OnConsolleMessage
onError
onResourceError
onResourceTimeout
事件()。也许有错误。Kepp记住,Google页面在PhantomJS中的外观与在桌面浏览器中的不同。打印页面内容以查看您需要的内容。既然您说“打印页面内容”,我假设这样做可以吗<代码>变量内容=page.content;fs.write(路径,内容,“w”)
。我在文档中搜索了
\u Rnb
,但没有找到。股票报价似乎采用了
表格格式
。格式不好。现在我需要选择项目。希望它能起作用。谢谢我不知道会有很大的不同,比如在“打印”版本中,它是一个
表格