python 正则表达式提取字典中的imUrl的value值
提取 'imUrl': 'http://ecx.images-amazon.com/images/I/31mCncNuAZL.jpg'这一项 展开
对于你给的字符串,可以被看作一个字典,所以可以按照键值来提取imUrl的value值,也可以用正则表达式提取imUrl的value值.
两种闭兆方法我都写出来了,你看看吧,要用哪种方法,你自己决定.(因为回答问题不能出现链接,所以我把imUrl的value值改成了'imUrl链接',意思是一样的)
第一种方法
data={'asin': '0000037214', 'related': {'also_viewed': ['B00JO8II76', 'B00DGN4R1Q', 'B00E1YRI4C']}, 'title': 'Purple Sequin Tiny Dancer Tutu Ballet Dance Fairy Princess Costume Accessory', 'price': 6.99, 'salesRank': {'Clothing': 1233557}, 'imUrl': 'imUrl链枝隐接', 'brand': 'Big Dreams','categories': [['Clothing, Shoes & Jewelry', 'Girls'], ['Clothing, Shoes & Jewelry', 'Novelty, Costumes & More', 'Costumes & Accessories', 'More Accessories', 'Kids & Baby']]}
print(data['imUrl'])
源代码(注意源代码的缩进)
第二种方法
import re
data="{'asin': '0000037214', 'related': {'also_viewed': ['B00JO8II76', 'B00DGN4R1Q', 'B00E1YRI4C']}, 'title': 'Purple Sequin Tiny Dancer Tutu Ballet Dance Fairy Princess Costume Accessory', 'price': 6.99, 'salesRank': {'Clothing': 1233557}, 'imUrl': 'imUrl链接', 'brand': 'Big Dreams','categories': [['Clothing, Shoes & Jewelry', 'Girls'], ['Clothing, Shoes & Jewelry', 'Novelty, Costumes & More', 'Costumes & Accessories', 'More Accessories', 'Kids & Baby']]}"
regex = r"'imUrl': '([\s\S]+?)'轿搭租"
match_obj = re.findall(regex,data)
for i in range(len(match_obj)):
print(match_obj[i])
源代码(注意源代码的缩进)
a='''{'asin': '0000037214', 'related': {'also_viewed': ['B00JO8II76'腔歼腔, 'B00DGN4R1Q', 'B00E1YRI4C']}, 'title': 'Purple Sequin Tiny Dancer Tutu Ballet Dance Fairy Princess Costume Accessory', 'price': 6.99, 'salesRank': {'Clothing'伍衫: 1233557}, 'imUrl': 'http://ecx.images-amazon.com/images/I/31mCncNuAZL.jpg', 'brand': 'Big Dreams'改桐, 'categories': [['Clothing, Shoes & Jewelry', 'Girls'], ['Clothing, Shoes & Jewelry', 'Novelty, Costumes & More', 'Costumes & Accessories', 'More Accessories', 'Kids & Baby']]}'''
print(re.findall(r" ('imUrl'): ('http://.*?')",a))