Install the corresponding library. I use PyV8.
Note that the function written in it must be enclosed ().
Copy codeThe Code is as follows:
Import PyV8
Class Test ():
Def js (self ):
Ctxt = PyV8.JSContext ()
Ctxt. enter ()
Func = ctxt. eval (''' (function () {return '###'})''')
Print func ()
Print '20140901'
If _ name _ = '_ main __':
Crawler = Test ()
Crawler. js ()
The output result is:
Copy codeThe Code is as follows:
>>>
###
213
>>>
Method for passing parameters to js Code
Copy codeThe Code is as follows:
Func = ctxt. eval (''' (function (a) {return encodeURIComponent ()})''')
Print func ()
A is the parameter to be passed, and encodeURIComponent is an encoding method in js.
I encountered this problem because some Chinese characters were encoded by the website's js using encodeURIComponent during post value transfer during crawler operations, which made it impossible to submit identifiable code, so think of this method to encode in the python program, and then pass the value, it can be a good solution to this problem.