2

How can I access a javascript variable with BeautifulSoup? I know the name of the variable

The variable is defined in a script tag:

<script>
    var variable_name = value;
</script>

For example, in the browser console I write window.variable_name and I get the value. How can I get an equivalent in Beautiful Soup?

Giancarlo Ventura
  • 849
  • 1
  • 10
  • 27
  • 1
    Since you said that the "variable is defined into a script tag in the page", you can use bs4 to get the – dot.Py Apr 11 '17 at 16:53
  • hi did you manage to solve your problem? I have similar issue and solved finding start and end of variable value but ideally would like to call the var name to make it more relaible.. – Je Je Aug 15 '20 at 15:54
  • Hi @NonoLondon, I just extract the text between `var variable_name` and `;` – Giancarlo Ventura Aug 22 '20 at 19:39

1 Answers1

1

You can't. BeautifulSoup is just a parser for DOM elements, it doesn't evaluate any code inside the page.

You need to "run" the page and access it while it's still "on", using, for example, Selenium, as explained in this post

Community
  • 1
  • 1
Thiago Cardoso
  • 503
  • 7
  • 13
  • The variable is defined into a script tag in the page. I edited the question – Giancarlo Ventura Apr 11 '17 at 16:42
  • Still, are you sure that is BeautifulSoup what you wanted, because what you will get is a text representation of variable's content if you manage to do that, what maybe won't work for you if the variable holds an object, for example. – Thiago Cardoso Apr 11 '17 at 18:13