简体   繁体   中英

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>

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM