python - 蟒蛇 | 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>
解决方案
我很确定你可以这样做:
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")
推荐阅读
- chef-infra - 我们可以在 chef 中使用 perl 或 python 来代替 Ruby
- c++ - 默认实现的多重继承不起作用 - 强制始终覆盖默认实现
- java - 递归子字符串搜索java
- excel - 当用户右键单击选项卡并删除时,在工作表上捕获单元格值?
- react-native - 是否可以将 codepush 与多个根组件一起使用?
- angular - 无法以角度 5 自动更新数据
- graphql - graphql 架构导入错误:在文档中找不到类型“联系人”
- apache - Apache2 + Mod_Evasive,仅适用于一个虚拟主机?
- xamarin.forms - xamarin 形式:圆形图像在 xamarin 形式 ios 中不起作用(显示为椭圆形)
- amazon-web-services - 网络接口不工作