如何解析包含 javascript 代码的 html

2023-12-31

如何解析大量使用 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 代码。

可能与如何使用 Node.js“执行”HTML+Javascript 页面 https://stackoverflow.com/questions/5222469/how-to-execute-htmljavascript-page-with-node-js但我真的需要 Node.js 和 JSDOM 吗?也稍微相关的是用于渲染 HTML 和 javascript 的 Python 库 https://stackoverflow.com/questions/126131/python-library-for-rendering-html-and-javascript但我对仅渲染纯 html 输出不感兴趣。


您可以使用Selenium http://seleniumhq.org/使用 python 详细说明here http://agiletesting.blogspot.com/2005/03/web-app-testing-with-python-part-2.html

Example:

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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何解析包含 javascript 代码的 html 的相关文章

随机推荐