我有使用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>
我只需要escramble_758()
的输出。我可以编写一个regex来解决整个问题,但我希望我的代码保持整洁。最好的选择是什么?
我正在快速浏览下面的库,但没有找到确切的解决方案。他们中的大多数人都试图模仿浏览器,使事情变得缓慢。
it's not yet possible to call a function defined in Javascript
)编辑:一个例子会很棒。。(赤骨就行了)
PyV8的另一个解决方案似乎是未维护的,并且依赖于旧版本的libv8。
PyMiniRacer它是v8引擎的包装器,它与新版本一起工作,并被积极维护。
pip install py-mini-racer
是的,你必须像其他人建议的那样用
return
替换document.write
您还可以使用Js2Py,它是用纯python编写的,能够执行javascript并将其转换为python。几乎支持整个JavaScript,甚至包括标签、getter、setter和其他很少使用的特性。
Js2Py的优点包括可移植性和与python的极易集成(因为基本上JavaScript被翻译成python)。
要安装:
使用PyV8,我可以做到这一点。但是,我必须用
return
替换document.write
,因为没有DOM,因此也没有document
。或者可以创建一个模拟文档对象
相关问题 更多 >
编程相关推荐