【问题标题】:How to parse html that includes javascript code如何解析包含 javascript 代码的 html
【发布时间】:2011-10-27 04:56:04
【问题描述】:

如何解析大量使用 javascript 的 html 文档?我知道python中有一些库可以解析静态xml / html文件,我基本上是在寻找一个程序或库(甚至是firefox插件),它可以读取html + javascript,执行javascript位并输出没有javascript的html代码因此,如果在浏览器中显示,它看起来会相同。

举个简单的例子

<a href="javascript:web_link(34, true);">link</a>

应替换为 javascript 函数返回的适当值,例如

<a href="http://www.example.com">link</a>

一个更复杂的例子是一个保存的 facebook html 页面,其中充斥着大量的 javascript 代码。

可能与 How to "execute" HTML+Javascript page with Node.js 但我真的需要 Node.js 和 JSDOM 吗?也稍微相关的是 Python library for rendering HTML and javascript 但我对只呈现纯 html 输出不感兴趣。

【问题讨论】:

标签: javascript python html


【解决方案1】:

PhantomJS可以使用Selenium加载

$ ipython

In [1]: from selenium import webdriver

In [2]: browser=webdriver.PhantomJS()

In [3]: browser.get('http://seleniumhq.org/')

In [4]: browser.title
Out[4]: u'Selenium - Web Browser Automation'

【讨论】:

    【解决方案2】:

    您可以将Selenium 与python 一起使用,详细说明here

    例子:

    import xmlrpclib
    
    # Make an object to represent the XML-RPC server.
    server_url = "http://localhost:8080/selenium-driver/RPC2"
    app = xmlrpclib.ServerProxy(server_url)
    
    # Bump timeout a little higher than the default 5 seconds
    app.setTimeout(15)
    
    import os
    os.system('start run_firefox.bat')
    
    print app.open('http://localhost:8080/AUT/000000A/http/www.amazon.com/')
    print app.verifyTitle('Amazon.com: Welcome')
    print app.verifySelected('url', 'All Products')
    print app.select('url', 'Books')
    print app.verifySelected('url', 'Books')
    print app.verifyValue('field-keywords', '')
    print app.type('field-keywords', 'Python Cookbook')
    print app.clickAndWait('Go')
    print app.verifyTitle('Amazon.com: Books Search Results: Python Cookbook')
    print app.verifyTextPresent('Python Cookbook', '')
    print app.verifyTextPresent('Alex Martellibot, David Ascher', '')
    print app.testComplete()
    

    【讨论】:

      【解决方案3】:

      来自Mozilla Gecko FAQ

      问。你能从 Unix shell 脚本调用 Gecko 引擎吗?您能否将其发送 HTML 并取回可能会发送到打印机的网页?

      A.不是很支持;不过,通过使用 Gecko 的嵌入 API 编写自己的应用程序,您可能会得到接近您想要的东西。请注意,如果屏幕上没有要呈现的小部件,目前无法打印。

      Embedding Gecko 在一个输出你想要的东西的程序中可能太重了,但至少你的输出会和它得到的一样好。

      【讨论】:

      猜你喜欢
      • 2012-07-27
      • 1970-01-01
      • 2015-01-22
      • 1970-01-01
      • 2019-09-26
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 2015-07-30
      相关资源
      最近更新 更多