Good day. How to pull out the data-params dictionary from this porridge?

<div class="subcategory-product-item product_data__gtm-js product_data__pageevents-js ddl_product" data-gtm-position="1" data-list-id="main" data-params='{"id":"393591","categoryId":214,"price":2290,"shortName":"Смартфон ARK Benefit S402 черный","categoryName":"Мобильные телефоны","brandName":"ARK"}' 

    1 answer 1

    First, you can use regular expressions to select the desired piece of text:

     import re s = '<div class="subcategory-product-item product_data__gtm-js product_data__pageevents-js ddl_product" data-gtm-position="1" data-list-id="main" data-params=\'{"id":"393591","categoryId":214,"price":2290,"shortName":"Смартфон ARK Benefit S402 черный","categoryName":"Мобильные телефоны","brandName":"ARK"}\'' j = re.match('.*data-params=\'(.*)\'', s).group(1) 

    And then convert the string to a dictionary using the ast library

     import ast d = ast.literal_eval(j) 

    or json :

     import json d = json.loads(j) 
    • Thanks, that is necessary! - moffire