【问题标题】:How to extract specific patterns from HTML with BeautifulSoup如何使用 BeautifulSoup 从 HTML 中提取特定模式
【发布时间】:2020-06-01 08:54:07
【问题描述】:

我正在尝试提取 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"
})

谢谢

【问题讨论】:

    标签: html python-3.x web-scraping beautifulsoup


    【解决方案1】:

    您可以使用.find()text= 参数,然后使用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"
    }
    

    编辑:如果你有多个&lt;scipt&gt; 标签,你可以这样做:

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

    【讨论】:

    • 谢谢 Andrej,但是如果我在这个 HTML 中有多个模式,如何管理您的解决方案?类似:for i in soup.find('script', text=lambda t: 'ProductsList' in t).contents[0]
    猜你喜欢
    • 2015-04-13
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    相关资源
    最近更新 更多