Reputation: 53
This is my code:
soup_detail.find_all("script",type="application/ld+json")[0].contents[0]
This is the output of above code:
'{ "@context": "http://schema.org", "@type": "Product", "name":"Men\'s Slippers Brief Design Rivet Decor All Match Fashion Wearable Shoes", "image":"", "description": "Material:Faux Leather", "brand":{ "@type": "Thing", "name": "" }, "aggregateRating": { "@type": "AggregateRating", "ratingValue": "5", "reviewCount": "1" }, "offers": { "@type": "Offer", "availability": "http://schema.org/InStock", "price": "10.99", "priceCurrency": "USD" } }'
How can I extract for example like "ratingValue": "5","reviewCount": "1" from this bs4.element.NavigableString?
Thanks in advance for the help.
Upvotes: 2
Views: 879
Reputation: 5344
j = '{ "@context": "http://schema.org", "@type": "Product", "name":"Men\'s Slippers Brief Design Rivet Decor All Match Fashion Wearable Shoes", "image":"", "description": "Material:Faux Leather", "brand":{ "@type": "Thing", "name": "" }, "aggregateRating": { "@type": "AggregateRating", "ratingValue": "5", "reviewCount": "1" }, "offers": { "@type": "Offer", "availability": "http://schema.org/InStock", "price": "10.99", "priceCurrency": "USD" } }'
j = json.loads(j)
print(j['@context'])
print(j['aggregateRating']['reviewCount'])
print(j['aggregateRating']['ratingValue'])
print(j.keys())
print(j.values())
output
http://schema.org
1
5
dict_keys(['@context', '@type', 'name', 'image', 'description', 'brand', 'aggregateRating', 'offers'])
dict_values(['http://schema.org', 'Product', "Men's Slippers Brief Design Rivet Decor All Match Fashion Wearable Shoes", '', 'Material:Faux Leather', {'@type': 'Thing', 'name': ''}, {'@type': 'AggregateRating', 'ratingValue': '5', 'reviewCount': '1'}, {'@type': 'Offer', 'availability': 'http://schema.org/InStock', 'price': '10.99', 'priceCurrency': 'USD'}])
Upvotes: 2
Reputation: 1123
import json
d = '{ "@context": "http://schema.org", "@type": "Product", "name":"Men\'s Slippers Brief Design Rivet Decor All Match Fashion Wearable Shoes", "image":"", "description": "Material:Faux Leather", "brand":{ "@type": "Thing", "name": "" }, "aggregateRating": { "@type": "AggregateRating", "ratingValue": "5", "reviewCount": "1" }, "offers": { "@type": "Offer", "availability": "http://schema.org/InStock", "price": "10.99", "priceCurrency": "USD" } }'
data = json.loads(d)
print(data['aggregateRating']['reviewCount']) #1
print(data['aggregateRating']['ratingValue']) #5
Upvotes: 2