Python USES PyV8 for javascript code sample sharing

  • 2020-04-02 13:18:51
  • OfStack

To install the appropriate libraries, I used PyV8

Note that the function is enclosed in ()


import PyV8
class Test():
      def js(self):        
   ctxt = PyV8.JSContext()        
   ctxt.enter()               
   func = ctxt.eval('''(function(){return '###'})''')        
   print func()        
   print '213'
if __name__ == '__main__':
     crawler = Test()    
  crawler.js()    

The output result is:


>>> 
###
213
>>>

Methods that pass arguments to js code


  func = ctxt.eval(''' (function(a){return encodeURIComponent(a)})''')
        print func(a)

A is the parameter to be passed. EncodeURIComponent is a coding method in js

I met this problem when I was doing crawler, during the process of post transmission, some Chinese characters were encoded by the js of the website using encodeURIComponent, which resulted in the failure to submit the identifiable code. Therefore, I thought of this method to encode in python program, and then transmit the value, which can solve this problem well


Related articles: