63

xpathを使用してクロールしているHTMLWebページがあります。etree.tostring特定のノードのは私にこの文字列を与えます:

<script>
<!--
function escramble_758(){
  var a,b,c
  a='+1 '
  b='84-'
  a+='425-'
  b+='7450'
  c='9'
  document.write(a+c+b)
}
escramble_758()
//-->
</script>

の出力が必要ですescramble_758()。全体を理解するために正規表現を書くことはできますが、コードを整理しておく必要があります。最良の選択肢は何ですか?

次のライブラリを圧縮していますが、正確な解決策が見つかりませんでした。それらのほとんどはブラウザをエミュレートしようとしており、カタツムリを遅くしています。

編集:例は素晴らしいでしょう..(barebonesは行います)

4

6 に答える 6

60

純粋なPythonで記述され、JavaScriptの実行とPythonへの変換の両方が可能なJs2Pyを使用することもできます。ラベル、ゲッター、セッター、その他のめったに使用されない機能でさえ、実質的にJavaScript全体をサポートします。

import js2py

js = """
function escramble_758(){
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write(a+c+b)
}
escramble_758()
""".replace("document.write", "return ")

result = js2py.eval_js(js)  # executing JavaScript and converting the result to python string 

Js2Pyの利点には、移植性とPythonとの非常に簡単な統合が含まれます(基本的にJavaScriptはPythonに変換されているため)。

インストールするには:

pip install js2py
于 2015-05-29T19:08:35.240 に答える
50

PyV8を使用して、これを行うことができます。ただし、 DOMがないため、に置き換える必要document.writeがあります。returndocument

import PyV8
ctx = PyV8.JSContext()
ctx.enter()

js = """
function escramble_758(){
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write(a+c+b)
}
escramble_758()
"""

print ctx.eval(js.replace("document.write", "return "))

または、モックドキュメントオブジェクトを作成することもできます

class MockDocument(object):

    def __init__(self):
        self.value = ''

    def write(self, *args):
        self.value += ''.join(str(i) for i in args)


class Global(PyV8.JSClass):
    def __init__(self):
        self.document = MockDocument()

scope = Global()
ctx = PyV8.JSContext(scope)
ctx.enter()
ctx.eval(js)
print scope.document.value
于 2012-04-13T07:07:56.930 に答える
23

PyV8は保守されておらず、古いバージョンのlibv8に依存しているように見えるため、もう1つの解決策があります。

PyMiniRacerこれはv8エンジンのラッパーであり、新しいバージョンで動作し、積極的に保守されています。

pip install py-mini-racer

from py_mini_racer import py_mini_racer
ctx = py_mini_racer.MiniRacer()
ctx.eval("""
function escramble_758(){
    var a,b,c
    a='+1 '
    b='84-'
    a+='425-'
    b+='7450'
    c='9'
    return a+c+b;
}
""")
ctx.call("escramble_758")

そして、はい、あなたは他の人が提案document.writeしたように置き換える必要がありますreturn

于 2018-03-21T09:16:52.757 に答える
4

js2pyコンテキストを使用してjsコードを実行し、モックドキュメントオブジェクトを使用してdocument.writeから出力を取得できます。

import js2py

js = """
var output;
document = {
    write: function(value){
        output = value;
    }
}
""" + your_script

context = js2py.EvalJs()
context.execute(js)
print(context.output)
于 2018-09-16T23:46:54.333 に答える
4

下のクロムをダウンロードして使用するrequests-htmlを使用できます。

from requests_html import HTML

html = HTML(html="<a href='http://www.example.com/'>")

script = """
function escramble_758(){
    var a,b,c
    a='+1 '
    b='84-'
    a+='425-'
    b+='7450'
    c='9'
    return a+c+b;
}
"""

val = html.render(script=script, reload=False)
print(val)
# +1 425-984-7450

これについての詳細はこちらをお読みください

于 2020-04-08T15:19:23.953 に答える
2

quickjsが出た後は、quickjsが最良のオプションになるはずです。ちょうどpip install quickjsそしてあなたは行く準備ができています。

READMEの例に基づいて変更します。

from quickjs import Function

js = """
function escramble_758(){
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write(a+c+b)
escramble_758()
}
"""

escramble_758 = Function('escramble_758', js.replace("document.write", "return "))

print(escramble_758())

https://github.com/PetterS/quickjs

于 2019-10-31T06:13:16.177 に答える