这是我的代码:
soup_detail.find_all(“ script”,type =“ application / ld + json”)[0] .contents [0]
这是上面代码的输出:
'{“ @context”:“ http://schema.org”,“ @type”:“产品”, “ name”:“男士拖鞋,简约设计,铆钉装饰,时尚十足 “耐磨鞋”,“图片”:“”,“说明”:“材质:人造皮革”, “ brand”:{“ @type”:“事物”,“ name”:“”},“ aggregateRating”:{ “ @type”:“ AggregateRating”,“ ratingValue”:“ 5”,“ reviewCount”:“ 1”}, “ offers”:{“ @type”:“ Offer”,“ availability”: “ http://schema.org/InStock”,“价格”:“ 10.99”,“ priceCurrency”:“美元” }}'
如何从此bs4.element.NavigableString中提取例如“ ratingValue”:“ 5”,“ reviewCount”:“ 1”?
预先感谢您的帮助。
答案 0 :(得分:2)
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())
输出
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'}])
答案 1 :(得分:2)
react-native