python - 使用 BeautifulSoup 解析脚本标签内的 json 文本
问题描述
我正在尝试使用 BeautifulSoup 提取 Python3@context
中 ( ) 元素中的文本。'script', type='application/ld+json'
我在一页中有多个脚本,我想获得上面 json 中列出的特定功能。
我尝试使用此代码:
data = soup.find_all('script', type='application/ld+json')
print(data)
这给了我所有脚本的完整提取内容,但我想在每个脚本的上下文中获得一个特定的功能。
功能示例:
{"name":"test","telephone":"600.212.0000","url":"https://test.com/test"}
对于这个例子,我想得到"url"
零件。
有人知道用 Python 做吗?
非常感谢您的帮助。
解决方案
您可以使用列表推导get()
:
data = soup.find_all('script', type='application/ld+json')
urls = [i.get('url') for i in data]
推荐阅读
- php - 如何对不在视图中的每个 $slug 进行自动重定向到 404
- sql - 在 Postgres 中使用连接指定窗口函数
- python-3.x - Python组合来自多个范围的数据
- ruby-on-rails - “多态关联不支持计算类”的关联问题。在多态属于 sti 中
- python - 错误:“来自:无法读取 /var/mail/django.utils.encoding”
- gitlab - 在提交消息中需要票号
- angular - Angular 8:HammerJs swipeRight 和 swipeLeft 在溢出内不起作用:auto
- excel - 并排的梳理表
- sql-server - 如何在 SQL Server Management Studio 中注册 SQL Server 服务器?
- c - 如何打印合并排序的执行时间