juju
juju

Reputation: 994

How can I use Beautiful Soup to get the value from a dictionary that is inside of a <script> tag

How can I use Beautiful Soup to get a value of productId from the following <script> tag

soup.find('script')
<script>
    gtmData.productData['34597834'] = {

        "productId": 1234,
        "foo": 1,
        "bar": 2,
    }
<script>

I want to retrieve the value of productId

Upvotes: 1

Views: 51

Answers (2)

Jack Fleeting
Jack Fleeting

Reputation: 24930

Another way, with no regex:

scr = """[your script above]"""
items = scr.split('{')[1].split('}')[0].split(',')
for item in items:
    if ':' in item:
        product = item.split(': ')
        print(product[0].strip(), product[1])

Output:

"productId" 1234
"foo" 1
"bar" 2

Upvotes: 1

you can print the soup object as a text.

import re
data = """gtmData.productData['34597834'] = {

    "productId": 1234,
    "foo": 1,
    "bar": 2,
}"""

print(re.search(r"productId\": (\d*)", data).group(1))

Output:

1234

Also there's several ways, such as load it in JSON to parse whatever you want.

Upvotes: 2

Related Questions