如何使用BeautifulSoup从HTML提取特定模式

标记

我试图提取具有重复模式的HTML的某些特定部分。

模式看起来像这样:

<script type="text/javascript">
    $(document).ready(function() {
        itemJS.ProductsList({"Status":"true",
            "description":"sku_01",
            "id": "00000001"
        });
    });
</script>

不幸的是,这个HTML里面有很多JavaScript,我只对上述模式感兴趣。使用BeatifulSoup库,我可以使用find.All功能获取HTML中的所有“ javascript”

soup.findAll('script', attrs={"type": "text/javascript"})

但是,如何仅提取这些特定模式?我想获得此“ dict”作为结果:

({"Status":"true",
 "description":"sku_01",
 "id": "00000001"
})

谢谢

安德烈·凯斯利(Andrej Kesely)

您可以使用.find()withtext=参数,然后使用re/json模块来解码数据。

例如:

import re
import json
from bs4 import BeautifulSoup

txt = '''
<script type="text/javascript">
    $(document).ready(function() {
        itemJS.ProductsList({"Status":"true",
            "description":"sku_01",
            "id": "00000001"
        });
    });
</script>'''

soup = BeautifulSoup(txt, 'html.parser')

# locate the <script>
t = soup.find('script', text=lambda t: 'ProductsList' in t).contents[0]

# get the raw string using `re` module
json_data = re.search(r'itemJS\.ProductsList\((.*?)\);', t, flags=re.DOTALL).group(1)

# decode the data
json_data = json.loads(json_data)

# print the data to screen
print(json.dumps(json_data, indent=4))

印刷品:

{
    "Status": "true",
    "description": "sku_01",
    "id": "00000001"
}

编辑:如果您有多个<scipt>标签,您可以执行以下操作:

import re
import json
from bs4 import BeautifulSoup

txt = '''
<script type="text/javascript">
    $(document).ready(function() {
        itemJS.ProductsList({"Status":"true",
            "description":"sku_01",
            "id": "00000001"
        });
    });
</script>

<script type="text/javascript">
    $(document).ready(function() {
        itemJS.ProductsList({"Status":"true",
            "description":"sku_02",
            "id": "00000002"
        });
    });
</script>
'''

soup = BeautifulSoup(txt, 'html.parser')

for script_tag in soup.find_all('script', text=lambda t: 'ProductsList' in t):
    json_data = re.search(r'itemJS\.ProductsList\((.*?)\);', script_tag.contents[0], flags=re.DOTALL).group(1)
    json_data = json.loads(json_data)
    print(json.dumps(json_data, indent=4))

打印:

{
    "Status": "true",
    "description": "sku_01",
    "id": "00000001"
}
{
    "Status": "true",
    "description": "sku_02",
    "id": "00000002"
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何提取使用模式从串号?

使用BeautifulSoup在HTML注释之间提取文本

在特定列中使用awk提取模式

使用BeautifulSoup提取html div类

在HTML表中的每个粗体标题使用BeautifulSoup之后,提取行中的特定列

如何使用Beautifulsoup检索此html中的特定内容?

如何使用BeautifulSoup提取特定的dl,dt列表元素

如何使用BeautifulSoup提取特定的字符串

如何从beautifulSoup中提取多个html标签?

如何使用BeautifulSoup在html注释标签中提取json?

如何通过BeautifulSoup从html提取值

如何使用BeautifulSoup从HTML页面提取Unicode文本?

如何使用Beautifulsoup提取html表

如何使用Python BeautifulSoup提取td HTML标签?

如何使用beautifulsoup从html标记的特定类中获取数据?

如何使用BeautifulSoup提取包含特定文本的div标签的内容

如何使用beautifulsoup提取值?

BeautifulSoup如何从HTML表格的特定列中提取数据。我的代码正在提取所有列

如何在beautifulsoup中删除特定模式内的所有html标签

使用 python BeautifulSoup 从 html 中提取特定内容

使用 BeautifulSoup 从带有特定字符串的 HTML 中提取表格

如何使用beautifulsoup从html中提取文本?

Beautifulsoup 如何从特定类中提取文本

如何使用 BeautifulSoup 从 HTML div 标签文件中提取文本?

如何使用 BeautifulSoup 从 HTML 中提取链接?

如何在 Python 中使用 BeautifulSoup 查找特定的 HTML 元素

如何使用 BeautifulSoup 提取 HTML 表格中的数据

如何使用beautifulsoup从网页上的特定分区中提取链接

使用 BeautifulSoup 从 HTML 中提取特定的 url