Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>

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.

Edit: An example will be great.. (barebones will do)

share|improve this question
    
Wait. Is this a dupe? Or was Pyjamas wrong enough that somebody deleted their answer? –  Erik Reppen Apr 13 '12 at 6:50
1  
May be PhantomJS can help or pyv8. –  RanRag Apr 13 '12 at 6:52
    
@ErikReppen I checked Pyjamas, it has no examples. –  jerrymouse Apr 13 '12 at 7:01
    
@RanRag request you to show an example if possible. –  jerrymouse Apr 13 '12 at 7:02
    
I deleted it because I realised it was pretty wrong. –  Karl Knechtel Apr 13 '12 at 7:48

1 Answer 1

up vote 16 down vote accepted

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
share|improve this answer
    
How do I install PyV8? When I do a python setup.py install, I get ERROR: you should set V8_HOME to the Google v8 folder, or download and build it first. <code.google.com/p/v8/>; On visiting this project I just see 2 downloads, benchmarks-v2.zip benchmarks-v1.zip. None of them have any py files. –  jerrymouse Apr 13 '12 at 7:34
    
I just use the exe installer because I'm using Window. I'm not sure about how to install it on other platforms. –  Dikei Apr 13 '12 at 7:36
2  
I am on mac.. Sometimes windows is so cool.. –  jerrymouse Apr 13 '12 at 7:38
    
I was facing the same problem and wow this is great. It works perfectly. Now I'm wondering how can I manage the function prompt?! help plz. –  Academia Jun 3 '12 at 11:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.