首页 > 解决方案 > 蟒蛇 | BS4 如何从中提取脚本

问题描述

简单的问题,我知道你们中的许多人都比我好得多。如何从中提取 json 代码,这样我就可以使用 bs4 了。在蟒蛇。谢谢,请不要降级:)

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

标签: pythonbeautifulsoup

解决方案


我很确定你可以这样做:

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

推荐阅读