Nowadays, there is at least one better tool, called slimit:

SlimIt is a JavaScript minifier written in Python. It compiles JavaScript into more compact code so that it downloads and runs faster.

SlimIt also provides a library that includes a JavaScript parser, lexer, pretty printer and a tree visitor.

Demo:

Imagine we have the following javascript code:

$.ajax({
    type: "POST",
    url: 'http://www.example.com',
    data: {
        email: '[email protected]',
        phone: '9999999999',
        name: 'XYZ'
    }
});

And now we need to get email, phone and name values from the data object.

The idea here would be to instantiate a slimit parser, visit all nodes, filter all assignments and put them into the dictionary:

from slimit import ast
from slimit.parser import Parser
from slimit.visitors import nodevisitor


data = """
$.ajax({
    type: "POST",
    url: 'http://www.example.com',
    data: {
        email: '[email protected]',
        phone: '9999999999',
        name: 'XYZ'
    }
});
"""

parser = Parser()
tree = parser.parse(data)
fields = {getattr(node.left, 'value', ''): getattr(node.right, 'value', '')
          for node in nodevisitor.visit(tree)
          if isinstance(node, ast.Assign)}

print fields

It prints:

{'name': "'XYZ'", 
 'url': "'http://www.example.com'", 
 'type': '"POST"', 
 'phone': "'9999999999'", 
 'data': '', 
 'email': "'[email protected]'"}
Answer from alecxe on Stack Overflow
🌐
GitHub
github.com › PiotrDabkowski › pyjsparser
GitHub - PiotrDabkowski/pyjsparser: Fast JavaScript parser for Python. · GitHub
Fast JavaScript parser - manual translation of esprima.js to python.
Starred by 256 users
Forked by 40 users
Languages   JavaScript 82.5% | Python 17.5%
Top answer
1 of 5
52

Nowadays, there is at least one better tool, called slimit:

SlimIt is a JavaScript minifier written in Python. It compiles JavaScript into more compact code so that it downloads and runs faster.

SlimIt also provides a library that includes a JavaScript parser, lexer, pretty printer and a tree visitor.

Demo:

Imagine we have the following javascript code:

$.ajax({
    type: "POST",
    url: 'http://www.example.com',
    data: {
        email: '[email protected]',
        phone: '9999999999',
        name: 'XYZ'
    }
});

And now we need to get email, phone and name values from the data object.

The idea here would be to instantiate a slimit parser, visit all nodes, filter all assignments and put them into the dictionary:

from slimit import ast
from slimit.parser import Parser
from slimit.visitors import nodevisitor


data = """
$.ajax({
    type: "POST",
    url: 'http://www.example.com',
    data: {
        email: '[email protected]',
        phone: '9999999999',
        name: 'XYZ'
    }
});
"""

parser = Parser()
tree = parser.parse(data)
fields = {getattr(node.left, 'value', ''): getattr(node.right, 'value', '')
          for node in nodevisitor.visit(tree)
          if isinstance(node, ast.Assign)}

print fields

It prints:

{'name': "'XYZ'", 
 'url': "'http://www.example.com'", 
 'type': '"POST"', 
 'phone': "'9999999999'", 
 'data': '', 
 'email': "'[email protected]'"}
2 of 5
23

ANTLR, ANother Tool for Language Recognition, is a language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions containing actions in a variety of target languages.

The ANTLR site provides many grammars, including one for JavaScript.

As it happens, there is a Python API available - so you can call the lexer (recognizer) generated from the grammar directly from Python (good luck).

Discussions

How do you parse a javascript script with python?
Use selenium to load the page, then simply use execute_script to get the variables you want. It's literally driver.execute_script("return myVar") More on reddit.com
🌐 r/webscraping
19
6
May 21, 2022
PyNarcissus - A Javascript parser in Python

I understand that this is a direct port of Narcissus, so the name was inevitable, but the whole reason it was called "Narcissus" in the first place is that it was a JavaScript parser written in JavaScript. Like staring at your own reflection? Get it?

Sorry. I'm a bastard.

More on reddit.com
🌐 r/programming
17
23
July 15, 2022
🌐
PyPI
pypi.org › project › esprima
esprima · PyPI
Esprima (esprima.org, BSD license) is a high performance, standard-compliant ECMAScript parser officially written in ECMAScript (also popularly known as JavaScript) and ported to Python.
      » pip install esprima
    
Published   Aug 24, 2018
Version   4.0.1
🌐
GitHub
github.com › differentmatt › filbert
GitHub - differentmatt/filbert: JavaScript parser of Python · GitHub
JavaScript-based Python parser.
Starred by 139 users
Forked by 26 users
Languages   JavaScript 82.5% | HTML 17.1% | CSS 0.4%
🌐
Reddit
reddit.com › r/webscraping › how do you parse a javascript script with python?
r/webscraping on Reddit: How do you parse a javascript script with python?
May 21, 2022 -

Dear webscrapers,

I'm scraping this website by intercepting the API with insomnia and requests. The problem is, I don't get a JSON but a javascript script containing javascript and multiple JSONS with the data I want. Does anybody know how I can parse the script to get whatever I want?

Any help appreciated, have been stuck on this for some time ^^'

🌐
Readthedocs
openerp-web-v7.readthedocs.io › en › stable
py.js, a Python expressions parser and evaluator — py.js 0.6 documentation
py.js is a parser and evaluator of Python expressions, written in pure javascript. py.js is not intended to implement a full Python interpreter, its specification document is the Python 2.7 Expressions spec (along with the lexical analysis part) as well as the Python builtins.
🌐
npm
npmjs.com › package › dt-python-parser
dt-python-parser - npm
In addition, several auxiliary methods are provided, for example, to filter comments of type # and """ in Python statements. ... Tip: The current Parser is the Javascript language version, if necessary, you can try to compile the Grammar file to other target languages
      » npm install dt-python-parser
    
Published   Apr 07, 2022
Version   0.9.0
Find elsewhere
🌐
PyPI
pypi.org › project › calmjs.parse
calmjs.parse · PyPI
As the Calmjs project provides a framework that produces and consume these module definitions, the the ability to have a comprehensive understanding of given JavaScript sources is a given. This goal was originally achieved using slimit, a JavaScript minifier library that also provided a comprehensive parser class that was built using Python Lex-Yacc (i.e.
      » pip install calmjs.parse
    
Published   Nov 08, 2025
Version   1.3.4
🌐
GitHub
github.com › codecombat › filbert
GitHub - codecombat/filbert: JavaScript parser of Python
JavaScript-based Python parser.
Starred by 5 users
Forked by 5 users
Languages   JavaScript 83.0% | HTML 16.6% | CSS 0.4% | JavaScript 83.0% | HTML 16.6% | CSS 0.4%
🌐
Tchut-Tchut Blog
beenje.github.io › blog › posts › parsing-javascript-rendered-pages-in-python-with-pyppeteer
Parsing JavaScript rendered pages in Python with pyppeteer | Tchut-Tchut Blog
June 2, 2018 - Pyppeteer allows you to do the same from Python. So there is no magic. You just let Chromium load and render the page with the latest JavaScript and browser features.
🌐
GitHub
github.com › rspivak › slimit
GitHub - rspivak/slimit: SlimIt - a JavaScript minifier/parser in Python
SlimIt is a JavaScript minifier written in Python. It compiles JavaScript into more compact code so that it downloads and runs faster. SlimIt also provides a library that includes a JavaScript parser, lexer, pretty printer and a tree visitor.
Starred by 552 users
Forked by 92 users
Languages   Python 100.0% | Python 100.0%
🌐
GitHub
github.com › Nykakin › chompjs
GitHub - Nykakin/chompjs: Parsing JavaScript objects into Python data structures
chompjs library was designed to bypass this limitation, and it allows to scrape such JavaScript objects into proper Python dictionaries: >>> import chompjs >>> >>> chompjs.parse_js_object("{'a': 'b'}") {'a': 'b'} >>> chompjs.parse_js_object('{a: "b"}') {'a': 'b'} >>> chompjs.parse_js_object('{"a": [1, 2, 3,]}') {'a': [1, 2, 3]} >>> chompjs.parse_js_object('{"a": .99}') {'a': 0.99} Internally chompjs use a parser written in C to iterate over raw string, fixing its issues along the way.
Starred by 217 users
Forked by 13 users
Languages   C 54.5% | Python 45.5% | C 54.5% | Python 45.5%
🌐
Python Programming
pythonprogramming.net › javascript-dynamic-scraping-parsing-beautiful-soup-tutorial
Scraping Dynamic Javascript Text
Thus, if you are reading the javascript-updated information, you will see the shinin message. If you don't then you will be ridiculed. If you open the page in your web browser, we'll see the shinin message, so we'll try in Beautiful Soup: import bs4 as bs import urllib.request source = urllib.request.urlopen('https://pythonprogramming.net/parsememcparseface/') soup = bs.BeautifulSoup(source,'lxml') js_test = soup.find('p', class_='jstest') print(js_test.text) y u bad tho?
🌐
LinkedIn
linkedin.com › pulse › chatgpt-javascript-parser-bipin-patwardhan
ChatGPT and JavaScript parser
August 6, 2023 - Below is a Python code snippet that uses the `pyparsing` library to parse JavaScript code: ```python from pyparsing import Word, alphas, nums, Group, Forward, Suppress, \ Keyword, OneOrMore, ZeroOrMore # Define the grammar for JavaScript using pyparsing identifier = Word(alphas, alphas + nums + "_$") number = Word(nums) string = (Suppress('"') + Word(alphas + nums + " ") + Suppress('"')) | \ (Suppress("'") + Word(alphas + nums + " ") + Suppress("'")) expression = Forward() function_call = identifier + Suppress("(") + ZeroOrMore(expression) \ + Suppress(")") expression << (identifier | number |
🌐
GitHub
github.com › Kronuz › esprima-python
GitHub - Kronuz/esprima-python: ECMAScript parsing infrastructure for multipurpose analysis · GitHub
Esprima (esprima.org, BSD license) is a high performance, standard-compliant ECMAScript parser officially written in ECMAScript (also popularly known as JavaScript) and ported to Python.
Starred by 266 users
Forked by 47 users
Languages   Python
🌐
GitHub
github.com › chlohal › python-parser
GitHub - chlohal/python-parser: A parser for Python in *pure* Javascript · GitHub
This is a naive Python parser in pure Javascript: no WASM, shell calls, or any non-JS.
Author   chlohal
🌐
Python
docs.python.org › 3 › library › html.parser.html
html.parser — Simple HTML and XHTML parser
>>> parser.feed('<style type="text/css">#python { color: green }</style>') Start tag: style attr: ('type', 'text/css') Data : #python { color: green } End tag : style >>> parser.feed('<script type="text/javascript">' ... 'alert("<strong>hello! &#9786;</strong>");</script>') Start tag: script attr: ('type', 'text/javascript') Data : alert("<strong>hello!
🌐
Reddit
reddit.com › r/programming › pynarcissus - a javascript parser in python
r/programming on Reddit: PyNarcissus - A Javascript parser in Python
July 15, 2022 - Perhaps now that it's Python staring at JavaScript in JavaScript's bedroom, it should be PyStalkerInYourHouse. ... Yeah. I mean, it supports the whole Javascript spec. Brendan Eich wrote the code, I just did language translation. I ran it on all of the Spidermonkey Javascript tests and compared the output to that of the original Narcissus parse trees and they're identical. ... I'm working on a JavaScript parser to make sure I really understand that part of my compilers class.
🌐
GitHub
github.com › joeedh › es5parse
GitHub - joeedh/es5parse: PyPLY JavaScript parser
A fully-functional JS parser, written in Python and PLY (a LALR compiler-compiler for Python).
Author   joeedh