Jsdom:如何使用' jsdom&#39 ;?打开带有数据的浏览器

时间:2017-07-30 13:18:55

标签: javascript node.js jsdom

我想开始使用' jsdom'包 jsdom:https://github.com/tmpvar/jsdom

我在文档中看到了以下代码段:

@tornado.gen.coroutine
def process_events_list(events):  
    try: 
        nc = NATS()
        parser = SafeConfigParser()
        conf = os.path.realpath(
        os.path.join(os.getcwd(),'ev_nats\\ev_nats.ini'))
        parser.read(conf)
        endpoints = ast.literal_eval(parser.get('Nats', 'Servers'))
        subject = parser.get('Nats', 'Subject')
        opts = {"servers": endpoints}
        **yield nc.connect(**opts)**  # wont connect return to main        
        for ev in events:
            yield nc.publish(subject, ev)
        yield nc.flush()
        log("Published")
    except Exception, e:
        log(e)

if __name__=='__main__': # if run directly, not called by event_dispatcher.py
   evt = ['1','2','3']
   tornado.ioloop.IOLoop.instance().run_sync(lambda : process_events_list(evt))

正如你所看到的,我可以使用' jsdom'从网站上提取数据" http://code.jquery.com/jquery-1.5.min.js&#34 ;.现在,我想把我所拥有的数据放在'窗口'变量,对它进行一些操作,然后打开浏览器并显示被操纵的数据。

我知道'打开'包可以帮我打开浏览器,但我必须给它一个URL作为参数。它无法处理窗口'宾语。你有什么想法?

1 个答案:

答案 0 :(得分:1)

我不确定你想要长期实现什么目标。我假设您在安装了浏览器的客户端计算机上运行node.js.如果这是真的,那么我会使用FS将DOM的内容保存到文件中,然后打开带有参数的浏览器来加载该html文件。

要获取内容,您应该只需运行jquery方法:

var html = $("html").html();

如何使用FS:

fs = require('fs');
fs.writeFile("/path/file.html", html)

要在浏览器中打开文件,您可以安装opn并运行它

const opn = require('opn');
opn ('file:///path/file.html')

如果没有opn,您可以使用类似于此问题的答案中的命令运行浏览器二进制文件:Execute a command line binary with Node.js