python - 用 BeautifulSoup 隔离脚本
问题描述
我已经在 BeautifulSoup 中加载了整个 HTML 页面。有没有办法可以隔离这个字典集合?
- 类型(黄色)在页面中只出现一次,没有重复
这是我用来导入 HTML 文件的代码(不能使用 urllib):from bs4 import BeautifulSoup
with open('/content/drive/My Drive/Colab Notebooks/Projects/20200710_StreetEasy_WebScraping/1.html') as f:
contents = f.read()
soup = BeautifulSoup(contents, 'lxml')
print(soup)
搜索 a 标签返回输出
a = soup.find_all('a')
a
[<a class="html-attribute-value html-resource-link" href="https://cdn-assets-s3.streeteasy.com/assets/manifest-c93475b02bd2409b4a52e21af023e5d5f489f19500d234a3660fe4d35069bbac.json" rel="noreferrer noopener" target="_blank">//cdn-assets-s3.streeteasy.com/assets/manifest-c93475b02bd2409b4a52e21af023e5d5f489f19500d234a3660fe4d35069bbac.json</a>,
<a class="html-attribute-value html-resource-link" href="https://browser.sentry-cdn.com/5.19.0/bundle.min.js" rel="noreferrer noopener" target="_blank">https://browser.sentry-cdn.com/5.19.0/bundle.min.js</a>,
<a class="html-attribute-value html-resource-link" href="https://cdn-assets-s3.streeteasy.com/assets/jquery-fe1be651ec56a9cc875a437f09db5b175cc6acf4b911bed0ef265955a099db55.js" rel="noreferrer noopener" target="_blank">//cdn-assets-s3.streeteasy.com/assets/jquery-fe1be651ec56a9cc875a437f09db5b175cc6acf4b911bed0ef265955a099db55.js</a>,
...
搜索脚本标签不返回任何输出
import re
scripts = soup.find_all("script")
scripts
[]
也许我在导入文档时做错了什么?
解决方案
您可以使用find_all中的字符串参数来过滤包含JSON的脚本标签@context
scripts = soup.find_all("script", string=re.compile("@context"))
然后遍历你的scripts
并在删除后加载 JSON//<![CDATA[
和//]]
推荐阅读
- amazon-web-services - AWS 的运行状况检查端点
- r - R中的拼写检查包问题
- image - 我在一个文件夹中有一组不同类别的图像,我如何将图像分类到它们各自的标签中
- macos - 尝试在 Mac 上运行 R Studio 时读取 R 脚本 () 时出错,系统错误 2
- javascript - Javascript。如何使用原始 JSON 对象(由另一个开发人员重新定义)
- c# - 网格布局组 - 孩子的位置和排序
- google-calendar-api - Google Calendar API:当用户回复邀请电子邮件时,与会者回复状态未更新
- javascript - jquery拆分数组,块的最后一个元素是下一个块的第一个元素
- gremlin - 节点类型数量的增加会影响图形数据库的性能吗?
- javascript - 在从 jQuery 到 JS 的转换中,数组填充了字符串而不是数字