ctrl_cheeb_del
ctrl_cheeb_del

Reputation: 1

How to find multiple strings with beautiful soup

When navigating a pages source code i am trying to get a list of the available size ids in the var meta object

var_meta ={
   "product":{
      "id":6718517543119,
      "gid":"gid:\/\/shopify\/Product\/6718517543119",
      "vendor":"Nike",
      "type":"1143 - Footwear - Mens NSW - Running",
      "variants":[
         {
            "id":39949577486543,
            "price":20000,
            "name":"Air Vapormax Evo - 7.5",
            "public_title":"7.5",
            "sku":"11536237"
         },
         {
            "id":39949577519311,
            "price":20000,
            "name":"Air Vapormax Evo - 8",
            "public_title":"8",
            "sku":"11536238"
         }

as you can see there is a list of ids inside of var meta but ive found the var meta with bs4 using
var_meta = soup.body.findAll(text='var meta')

but my question is how can I obtain the ids inside of the var meta and return them?

thank you

Upvotes: 0

Views: 194

Answers (1)

Spectre Yen
Spectre Yen

Reputation: 39

var_meta can be called upon similar to how you'd do that with a dictionary. So in order to access the values of the keys within var_meta, you will first get the values within products by doing:

var_meta["product"]

Then, you will get the values within the variants:

var_list = var_meta["product"]["variants"]

Considering that this is a list with multiple items, you will loop through each item and within each item, you will search for the value of the 'id' key.

for item in var_list:
    id = item['id']
    print(id)

Full code:

var_meta ={
   "product":{
      "id":6718517543119,
      "gid":"gid:\/\/shopify\/Product\/6718517543119",
      "vendor":"Nike",
      "type":"1143 - Footwear - Mens NSW - Running",
      "variants":[
         {
            "id":39949577486543,
            "price":20000,
            "name":"Air Vapormax Evo - 7.5",
            "public_title":"7.5",
            "sku":"11536237"
         },
         {
            "id":39949577519311,
            "price":20000,
            "name":"Air Vapormax Evo - 8",
            "public_title":"8",
            "sku":"11536238"
         }
      ]
   }
}


var_list = var_meta["product"]["variants"]
for item in var_list:
    id = item["id"]
    print(id)

Upvotes: 2

Related Questions