S0V3T
S0V3T

Reputation: 23

Python | BS4 How to extract script from <script></script>

Easy question, I know many of you are a lot better than me. How do I extract json code out of it, so I can work with bs4 on it. In python. Thank you, please don't downgrade:)

 <script type="text/javascript">window._sharedData = {"config":{"csrf_token":"kkEzPAyLVqXzZaS3xITIlWtkCF6tUi2m","viewer":null,"viewerId":null},"country_code":"PL","language_code":"pl","locale":"pl_PL","entry_data":{"ProfilePage":[{"logging_page_id"..."version":"10"},"is_dev":false,"rollout_hash":"fd856a1a782f","bundle_variant":"es6","frontend_env":"prod"};</script>

Upvotes: 0

Views: 288

Answers (1)

Andrej
Andrej

Reputation: 3235

I'm pretty sure you can do it this way:

from bs4 import BeautifulSoup
import requests

response = requests.get(URL)
soup = BeautifulSoup(response.text, "lxml")

element = soup.Find("script", type = "text/javascript")
json = element.get("window._sharedData")

Upvotes: 2

Related Questions