Extracting data from script tag using BeautifulSoup in Python

前端 未结 2 825
小蘑菇
小蘑菇 2020-12-21 07:09

I want to extract \"SNG_TITLE\" and \"ART_NAME\" values from the code in \"script\" tag using BeautifulSoup in Python. (the whole script is too long to paste)



        
2条回答
  •  时光取名叫无心
    2020-12-21 07:38

    If my understanding is correct, you want only the script element with "SNG_TITLE" in it.

    You can use re and get only the script element with the fields of your interest as follows:

    import requests
    from bs4 import BeautifulSoup
    import re
    
    base_url = 'https://www.deezer.com/en/profile/1589856782/loved'
    
    r = requests.get(base_url)
    
    soup = BeautifulSoup(r.text, 'html.parser')
    
    user_name = soup.find(class_='user-name')
    print(user_name.text)
    
    for script in soup(text=re.compile(r'SNG_TITLE' )):
        print(script.parent)
    

    EDIT:

    @furas answer is the complete solution using json to find the 'SNG_TITLE' and 'ART_TITLE'. My answer help you find only the script with 'SNG_TITLE'. You can combine both to get better code.

提交回复
热议问题