Search code examples
pythonbeautifulsouppython-re

how to get a specific value in a script inside an html?


I have an HTML file, and this file contains several scripts specifically in the last <script></script> contains a value that I would like to get

I need to get the hash value found here

extend(cur, { "hash": "13334a0e457f0793ec", "loginHost": "login", "sureBoxText": false, "strongCode": 0, "joinParams": false, "validationType": 3, "resendDelay": 120, "calledPhoneLen": 4, "calledPhoneExcludeCountries": [1, 49, 200] });

How can I do this? I've tried using soup but I think I'm doing it wrong. I really need to complete this, if you can help me I will be eternally grateful. I tried using the re library but I don't know how to use it. ex

re.search(html, "hash: (*?),")

is there any way to do a search like this?


Solution

  • You can use .group() to access a captured group:

    import re
    
    data = """extend(cur, { "hash": "13334a0e457f0793ec", "loginHost": "login", "sureBoxText": false, "strongCode": 0, "joinParams": false, "validationType": 3, "resendDelay": 120, "calledPhoneLen": 4, "calledPhoneExcludeCountries": [1, 49, 200] });"""
    
    print(re.search(r'{ "hash": "(.*?)",', data).group(1))
    

    Output:

    13334a0e457f0793ec
    

    Regular expression explanation:

    enter image description here