Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve a JavaScript variable using Python? [closed]


I'm trying to retrieve a Javascript variable using Python and I'm having some issues...

Here is what the variable looks like :

<script type="text/javascript">
var exampleVar = [
    {...},
    {...},
    {
        "key":"0000",
        "abo":
            {
                "param1":"1"
                "param2":"2"
                "param3":
                    [
                        {
                            "param3a1":"000"
                            "param3a2":"111"
                        },
                        {
                            "param3b1":"100"
                            "param3b2":"101"
                        }
                    ]
             }
]
</script>

After some research, I discovered that its content was in the JSON format, and I'm new to it...

My problem now is that I would like to retrieve the value of "param3b1" (for example) to use it in my Python program.
How do I do this in Python ?
Thanks !

like image 916
sylvelk Avatar asked Oct 07 '14 16:10

sylvelk


People also ask

Where is Javascript variable stored?

Variables in JavaScript (and most other programming languages) are stored in two places: stack and heap. A stack is usually a continuous region of memory allocating local context for each executing function. Heap is a much larger region storing everything allocated dynamically.


Video Answer


1 Answers

Step by step this is what you need to do.

  1. extract the json string from the file/html string. you need to get the string between the <script> tags first, and then the variable definition
  2. extract the parameter from the json string.

Here is a demo.

from xml.etree import ElementTree

import json
tree = ElementTree.fromstring(js_String).getroot() #get the root
#use etree.find or whatever to find the text you need in your html file
script_text = tree.text.strip()

#extract json string
#you could use the re module if the string extraction is complex
json_string = script_text.split('var exampleVar =')[1]
#note that this will work only for the example you have given.
try:
    data = json.loads(json_string)
except ValueError:
    print "invalid json", json_string
else:
    value = data['abo']['param3']['param3b1']
like image 163
srj Avatar answered Oct 26 '22 23:10

srj