-1

I have this html code which returns the value of PI

<!DOCTYPE html>
<html>
<body>

<p>This example calls a function which returns the value of PI:</p>

<p id="demo"></p>

<script>
function myFunction() {
    return Math.PI;
}

document.getElementById("demo").innerHTML = myFunction();
</script>

</body>
</html>

When I use python requests lib, I get only the source code exactly like above but not the values of Math.PI.

I want to extract only the values of PI and how do I do using python ?

Ben
  • 109
  • 6
  • 1
    This post has information about executing js using python: [Executing Javascript from Python](https://stackoverflow.com/questions/10136319/executing-javascript-from-python) – Shay Lempert Dec 17 '18 at 13:19
  • What are you using to parse HTML ? – LaSul Dec 17 '18 at 13:25
  • I am using Beautifulsoup but I am not sure how to get the data of dynamically processed function in javascript. – Ben Dec 17 '18 at 13:27
  • Please provide the lines of code where you are using `requests` and `BeautifulSoup` – Mortz Dec 17 '18 at 13:29

2 Answers2

0

You can use js2py library from Piotr Dabkowski.

Just use a parser or a regex to get the function you want and then :

#pip install js2py

import js2py

js = """
function myFunction() {
    return Math.PI;
}
"""

myFunction = js2py.eval_js(js)

myFunction()

#Returns
3.141592653589793
LaSul
  • 2,231
  • 1
  • 20
  • 36
  • thank you but how do I make it work if for eg. the value of PI is constantly updated from server not from a static function. In my case that sample html code is in server and im using python requests to extract the value (PI) which is updated from server. – Ben Dec 17 '18 at 13:41
  • Lol, you didn't ask for that in your question man. Just validate this one first, and then modify your question or create another one for you problem. – LaSul Dec 17 '18 at 13:53
  • But the only solution for your problem is to run your script constantly either with cronjob, either with loop. This depends of your problem. For more the Math.PI function in Javascript is a constant, so the value of PI will not change. So could you please provide a sample of the real code if it isn't or something really close or we won't be able to help you further – LaSul Dec 17 '18 at 13:55
0

You need to look at the html code after it was run in a browser.
In this case using an headless browser would work. There are plenty of them to choose from, like activesoup

B. Go
  • 1,436
  • 4
  • 15
  • 22