Run Obfuscated JavaScript Directly in Python with ExecJS – A Quick Guide
This article explains why Python developers often need to run JavaScript—especially obfuscated code—during web scraping, demonstrates how to use the ExecJS library to execute both simple and heavily obfuscated JavaScript from Python, and shows that the approach works seamlessly without de‑obfuscation.
Introduction
In web scraping, Python is the dominant language, but many sites protect data with JavaScript obfuscation, making it hard to extract values directly.
Why Execute JavaScript from Python?
Anti‑scraping measures often rely on client‑side JavaScript, especially obfuscated code that generates dynamic data. Re‑implementing the logic in Python is tedious, so being able to run the original JS and obtain its return value simplifies the process.
What Is JavaScript Code Obfuscation?
Normal Code
The following simple function returns a formatted date string.
function formatDate(now) {
var now = new Date(1230999938);
var year=now.getFullYear();
var month=now.getMonth()+1;
var date=now.getDate();
var hour=now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();
return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
}Running this code produces the expected date output.
Obfuscated Code
After passing the same function through an online obfuscator, the code becomes unreadable but still functional.
function formatDate(mz1){var KkkGDiH2=new window["\x44\x61\x74\x65"](1230999938);var tsk3=KkkGDiH2['\x67\x65\x74\x46\x75\x6c\x6c\x59\x65\x61\x72']();var YMreyP4=KkkGDiH2['\x67\x65\x74\x4d\x6f\x6e\x74\x68']()+1;var Ozo5=KkkGDiH2['\x67\x65\x74\x44\x61\x74\x65']();var QMYEc$eD6=KkkGDiH2['\x67\x65\x74\x48\x6f\x75\x72\x73']();var JfXVV_Akq7=KkkGDiH2['\x67\x65\x74\x4d\x69\x6e\x75\x74\x65\x73']();var $mP8=KkkGDiH2['\x67\x65\x74\x53\x65\x63\x6f\x6e\x64\x73']();return tsk3+"\x2d"+YMreyP4+"\x2d"+Ozo5+" "+QMYEc$eD6+"\x3a"+JfXVV_Akq7+"\x3a"+$mP8Even though the code is heavily obfuscated, it can still be executed and returns the same result.
Python Package ExecJS
ExecJS is a Python library that provides a simple interface to run JavaScript code using an installed JavaScript runtime such as Node.
Installation
pip3 install PyExecJSRunning JavaScript from Python
Example with a straightforward function:
import execjs
ctx = execjs.compile("""
function add(x, y) {
return x + y;
}
""")
print(ctx.call("add", 1, 2))Executing Obfuscated JavaScript
The same approach works with obfuscated code:
import execjs
ctx = execjs.compile("""
function add(bi1,Pl$2){return bi1+Pl$2}
""")
print(ctx.call("add", 1, 2))Thus, even heavily obfuscated JavaScript can be executed from Python without de‑obfuscation.
Conclusion
Anti‑scraping techniques will continue to evolve, but tools like ExecJS allow Python developers to run JavaScript directly, turning a difficult obstacle into a straightforward solution.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
