2011-08-15 10 views
5

javascriptを大量に使用するhtmlドキュメントを解析するにはどうすればよいですか?私は静的なxml/htmlファイルを解析することができるPythonのいくつかのライブラリがあることを知っていると私は基本的にはhtml + javascriptを読むプログラムやライブラリ(またはfirefoxのプラグイン)を探して、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出力だけをレンダリングすることには興味がありません。

+0

どちらのJavaScriptランタイムを取得し、それを使って何かを整理、コードを分析して、最終的に何が起こるかを検討します(強くサイトごとの構成)。 –

+0

http://stackoverflow.com/questions/19465510/how-to-parse-a-webpage-that-includes-javascript?rq=1 – gliptak

答えて

2

Mozilla Gecko FAQから:

Q.あなたは、UnixシェルスクリプトからGeckoエンジンを呼び出すことができますか? HTMLを送信して、プリンタに送信される可能性のあるWebページを取得できますか?

A.実際にはサポートされていません。 Geckoの組み込みAPIを使用して独自のアプリケーションを作成することで、あなたが望むものに近いものを得ることができます。現在のところ、レンダリングするウィジェットを画面に表示することはできません。

Embedding Geckoあなたが望むものを出力するプログラムが重すぎるかもしれませんが、少なくともあなたの出力は得られるほど良いでしょう。

+0

このレシピを追加することもできます:http://siliconforks.com/doc/parsing -javascript-with-spidermonkey / –

3

あなたは詳細なhere

例としてPythonでSeleniumを使用することができます。

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() 
0

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' 
関連する問題