Python | BS4如何从<script> </script>提取脚本

时间:2020-07-15 19:14:04

标签: python beautifulsoup

一个简单的问题,我知道你们中的许多人比我强很多。 如何从中提取json代码,以便可以在其上使用bs4。在python中。谢谢,请不要降级:)

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

1 个答案:

答案 0 :(得分:2)

我很确定你可以这样:

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