Is there any way to get the executed javascript contents from a webpage? I have tried requests + BeautifulSoup, mechanize, these yield me with "source code" of the webpage and not the executed javascript. For example, this website :- http://listen.tidal.com/login
As you can see, in the source code, there is un-execute JS, but, when you inspect the element, you'll see the executed code.
Now, is there any way I could get that EXECUTED code in python? Hints please, because I have tried emulating a browser using mechanize and it does the same like reuqests. Thank You
In fact, JavaScript engine is needed for execution of javascript. Python is a language with its own interpreter(compiler!) to execute python code. These are two different technology. So if you want to execute javascript from python, python must have api or sort of bindings that interacts with the engine executes javascript. Fortunately python has interactivity with several JS Engine for implementing web related works(testing etc.). This interoperable JS can be divided into two groups as below-
Simple execution example of JS in selenium python as below-
from selenium import webdriver
#define driver- firefox, chrome or phantomjs etc.
driver = webdriver.Firefox()
#Open the url
driver.get('https://www.google.com')
#see how javascript simple alert is being executed
driver.execute_script("alert('hello world');")
#close the driver i.e. closing opened Firefox instance!
driver.close()
Just to highlight - Python doesn't execute your Js code, but runtime does. Here is an example of the python module that picks available runtime and evaluates code for you.
Look at PyExecJS, here you can find some examples, but take into account that it might not contain any browser APIs like DOM, Html5 Api, etc. It's mostly based on js engine capabilities.
Another big question, what is the reason to evaluate code in the python?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With