jer*_*use 46 javascript python screen-scraping
我有使用xpath爬行的HTML网页.在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>
Run Code Online (Sandbox Code Playgroud)
我只需要输出escramble_758()
.我可以写一个正则表达式来弄清楚整个事情,但我希望我的代码保持整洁.什么是最好的选择?
我正在浏览以下库,但我没有看到确切的解决方案.他们中的大多数都试图模仿浏览器,使事情变得缓慢.
it's not yet possible to call a function defined in Javascript
)编辑:一个例子将是伟大的..(准系统会做)
Pio*_*ski 42
你也可以使用用纯python编写的Js2Py,并且能够执行和转换javascript到python.几乎支持整个JavaScript甚至标签,getter,setter和其他很少使用的功能.
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
Run Code Online (Sandbox Code Playgroud)
Js2Py的优点包括可移植性和非常容易与python集成(因为基本上JavaScript正在被转换为python).
安装:
pip install js2py
Run Code Online (Sandbox Code Playgroud)
Kie*_*ong 36
使用PyV8,我可以做到这一点.但是,我必须更换document.write
,return
因为没有DOM,因此没有document
.
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 "))
Run Code Online (Sandbox Code Playgroud)
或者您可以创建一个模拟文档对象
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
Run Code Online (Sandbox Code Playgroud)
Die*_*now 14
另一个解决方案是PyV8似乎没有维护,并且依赖于旧版本的libv8.
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")
Run Code Online (Sandbox Code Playgroud)
是的,你必须更换document.write
与return
为他人建议
Mir*_*rko 10
您可以使用 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)
Run Code Online (Sandbox Code Playgroud)
您可以使用requests-html它将在下面下载并使用 chromium。
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
Run Code Online (Sandbox Code Playgroud)
更多相关内容请阅读此处
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())
Run Code Online (Sandbox Code Playgroud)
https://github.com/PetterS/quickjs