在Python中将JavaScript呈现为HTML? [英] Render JavaScript to HTML in Python?

查看:139
本文介绍了在Python中将JavaScript呈现为HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么



我的网路应用程式是透过Google的 AngularJS



我想要生成我的网页的静态版本。



为什么



像Google这样的Web刮板执行并渲染JavaScript;但不要以与静态等价物相同的方式处理内容。



参考文献:



How



不知道具体如何 - 这就是我问的原因 - 但我想要访问与您的浏览器的检查元素相同的来源;而不是来源: Ctrl + U (查看页面源代码)显示。



呈现页面; '吐出'HTML + CSS;我会把这些'生成'的文件放到我的网络服务器上。然后,'cron'作业会定期重新生成这些文件。

这些静态文件随后将被替换为动态文件;当JavaScript被禁用和/或当一个刮板'访问'该网站时。

解决方案

这是一个解决方案,怀疑我可以找到一个可以运行它的公共PaaS云:

 导入spynner 

if __name __ =='__ main__':
url =http://angular.github.com/angular-phonecat/step-10/app/#/phones
browser = spynner.Browser( )
browser.create_webview(True)
browser.load(url,load_timeout = 60)
打印browser._get_html()
#^可以将它传递给一个文件,我的服务器或返回它作为一个字符串
browser.close()

包:< a href =https://github.com/makinacorpus/spynner/ =nofollow> Spynner(在Github上)


What

My web-app is made dynamic through Google's AngularJS.

I want static versions of my pages to be generated.

Why

Web-scrapers like Google's execute and render the JavaScript; but don't treat the content the same way as their static equivalents.

References:

How

Not sure exactly how—which is why I'm asking—but I want to access the same source that your browser's 'inspect element' presents; rather than the source that: Ctrl+U (View page source) shows.

Once I have a script which renders the page; 'spitting' out the HTML+CSS; I will place those 'generated' files on my web-server. A 'cron' job will then be scheduled to regenerate the files at regular intervals.

These static files will subsequently be served instead of the dynamic ones; when JavaScript is disabled and/or when a scraper 'visits' the site.

解决方案

Here is one solution, however I very much doubt I'll be able to find a public PaaS cloud which can run it:

import spynner

if __name__=='__main__':
    url = "http://angular.github.com/angular-phonecat/step-10/app/#/phones"
    browser = spynner.Browser()
    browser.create_webview(True)
    browser.load(url, load_timeout=60)
    print browser._get_html()
    # ^ Can pipe this to a file, POST it to my server or return it as a string
    browser.close()

Package: Spynner (on Github)

这篇关于在Python中将JavaScript呈现为HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆