title: Extract data from <script> with beautifulsoup
body: <p>I'm trying to scrape some data with Python and Beautifulsoup. I know how to get the text from the script tag. The data between [ ] is valid json. </p>
  
  <pre><code>&lt;script&gt;
      dataLayer = 
  [  
    {  
    "p":{  
           "t":"text1",
           "lng":"text2",
           "vurl":"text3"
         },
    "c":{  },
    "u":{  },
    "d":{  },
    "a":{  }
    }
  ]
  &lt;/script&gt;
  </code></pre>
  
  <p>I've read this response and it almost does what I want:
  <a href="https://stackoverflow.com/questions/26192727/extract-content-of-script-with-beautifulsoup">Extract content of &lt;Script with BeautifulSoup</a></p>
  
  <p>Here is my code:</p>
  
  <pre><code>import urllib.request
  from bs4 import BeautifulSoup
  import json
  
  url = "www.example.com"
  html = urllib.request.urlopen(url)
  soup = BeautifulSoup(html, "html.parser")
  raw_data = soup.find("script")
  </code></pre>
  
  <p>I would then ideally do:</p>
  
  <pre><code>json_dict = json.loads(raw_data)
  </code></pre>
  
  <p>And access the data through the dictionary. But this is not working because of </p>
  
  <pre><code>"&lt;script&gt; dataLayer =" 
  </code></pre>
  
  <p>preceding the valid json and the script tag at the end. I've tried trimming the raw_data as a string, like this: </p>
  
  <pre><code>raw_data[20:]
  </code></pre>
  
  <p>But this didn't work because the soup object is not a string. </p>
  
  <p>How can I get the raw_data variable to contain ONLY the text between the block quotes [ ]?</p>
  
  <p>EDIT: this seems to work. It avoids regex and solves the problem of the trailing chars as well. Thanks for your suggestions.</p>
  
  <pre><code>url = "www.example.com"
  html = urllib.request.urlopen(url)
  soup = BeautifulSoup(html, "html.parser")
  
  # get the script tag data and convert soup into a string
  data = str(soup.find("script"))
  
  # cut the &lt;script&gt; tag and some other things from the beginning and end to get valid JSON
  cut = data[27:-13]
  
  # load the data as a json dictionary
  jsoned = json.loads(cut)
  </code></pre>