从Python执行Javascript [英] Executing Javascript from Python
问题描述
我有使用xpath抓取的HTML网页。某个节点的 etree.tostring
为我提供了这个字符串:
I have HTML webpages that I am crawling using xpath. The etree.tostring
of a certain node gives me this string:
<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()
。我可以写一个正则表达式来弄清楚整个事情,但我希望我的代码保持整洁。最好的选择是什么?
I just need the output of escramble_758()
. I can write a regex to figure out the whole thing, but I want my code to remain tidy. What is the best alternative?
我正在浏览以下库,但我没有看到确切的解决方案。他们中的大多数都试图模仿浏览器,使事情变得缓慢。
I am zipping through the following libraries, but I didnt see an exact solution. Most of them are trying to emulate browser, making things snail slow.
- http://code.google.com/p/python-spidermonkey/ (清楚地说
它还无法调用Javascript中定义的函数
) - http://code.google.com/p / webscraping / (看不到Javascript的任何内容,我可能错了)
- http://pypi.python.org/pypi/selenium (模拟浏览器)
- http://code.google.com/p/python-spidermonkey/ (clearly says
it's not yet possible to call a function defined in Javascript
) - http://code.google.com/p/webscraping/ (don't see anything for Javascript, I may be wrong)
- http://pypi.python.org/pypi/selenium (Emulating browser)
编辑:一个例子将是伟大的..(准系统会做)
推荐答案
使用 PyV8 ,我可以这样做。但是,我必须用 return
替换 document.write
,因为没有DOM,因此没有文件
。
Using PyV8, I can do this. However, I have to replace document.write
with return
because there's no DOM and therefore no 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 "))
或者你可以创建一个模拟文档对象
Or you could create a mock document object
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
这篇关于从Python执行Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!