如匹配内容
text='result({
"productCommentSummary":{
"SkuId": "1097069",
"ProductId": "1097069",
"DefaultGoodCount": "258614",
"DefaultGoodCountStr": "25万+",
"ShowCount": "13520",
"ShowCountStr": "1.3万+",
"CommentCountStr": "34万+",
"CommentCount": "346312",
"AverageScore": "5",
"GoodCountStr": "12万+",
"GoodCount": "121967",
"AfterCount": "1059",
"AfterCountStr": "1000+",
"PlusCount": "0",
"PlusCountStr": "0",
"OneYear": "0",
"VideoCount": "997",
"VideoCountStr": "900+",
"GoodRate": "0.97",
"GoodRateShow": "97",
"GoodRateStyle": "145",
"GeneralCountStr": "1500+",
"GeneralCount": "1519",
"GeneralRate": "0.012",
"GeneralRateShow": "1",
"GeneralRateStyle": "2",
"PoorCountStr": "1500+",
"PoorCount": "1572",
"PoorRate": "0.018",
"PoorRateShow": "2",
"PoorRateStyle": "3",
"SensitiveBook": "0"
},
"hotCommentTagStatistics":[
{
"id": "4718ed7ec8af7f16",
"name": "清爽舒适",
"rid": "4718ed7ec8af7f16",
"count": "232",
"type": "4",
"canBeFiltered": true,
"stand": "1",
"ckeKeyWordBury": "eid=100^^tagid=4718ed7ec8af7f16^^pid=30006^^sku=1097069^^sversion=1000^^token=3655b7ed53a7de05"
}
,
{
"id": "d1a61b29c4d11818",
"name": "分量足够",
"rid": "d1a61b29c4d11818",
"count": "2",
"type": "4",
"canBeFiltered": true,
"stand": "1",
"ckeKeyWordBury": "eid=100^^tagid=d1a61b29c4d11818^^pid=30006^^sku=1097069^^sversion=1000^^token=95fdbedf7a09f343"
}
{
"id": "9737cdd8cafd7493",
"name": "不会粘手",
"rid": "9737cdd8cafd7493",
"count": "1",
"type": "4",
"canBeFiltered": true,
"stand": "1",
"ckeKeyWordBury": "eid=100^^tagid=9737cdd8cafd7493^^pid=30006^^sku=1097069^^sversion=1000^^token=bf26e154e8b8ccae"
}
,
{
"id": "63ad1a9d2a96b819",
"name": "安全环保",
"rid": "63ad1a9d2a96b819",
"count": "1",
"type": "4",
"canBeFiltered": true,
"stand": "1",
"ckeKeyWordBury": "eid=100^^tagid=63ad1a9d2a96b819^^pid=30006^^sku=1097069^^sversion=1000^^token=480bafbb3298af4c"
}
]
})'
现在要使用re 匹配出 CommentCount、GoodCount、GoodRate的值,其中GoodRate是浮点数,代码如下:
import re
text='result({
"productCommentSummary":{
"SkuId": "1097069",
"ProductId": "1097069",
"DefaultGoodCount": "258614",
"DefaultGoodCountStr": "25万+",
"ShowCount": "13520",
"ShowCountStr": "1.3万+",
"CommentCountStr": "34万+",
"CommentCount": "346312",
"AverageScore": "5",
"GoodCountStr": "12万+",
"GoodCount": "121967",
"AfterCount": "1059",
"AfterCountStr": "1000+",
"PlusCount": "0",
"PlusCountStr": "0",
"OneYear": "0",
"VideoCount": "997",
"VideoCountStr": "900+",
"GoodRate": "0.97",
"GoodRateShow": "97",
"GoodRateStyle": "145",
"GeneralCountStr": "1500+",
"GeneralCount": "1519",
"GeneralRate": "0.012",
"GeneralRateShow": "1",
"GeneralRateStyle": "2",
"PoorCountStr": "1500+",
"PoorCount": "1572",
"PoorRate": "0.018",
"PoorRateShow": "2",
"PoorRateStyle": "3",
"SensitiveBook": "0"
},
"hotCommentTagStatistics":[
{
"id": "4718ed7ec8af7f16",
"name": "清爽舒适",
"rid": "4718ed7ec8af7f16",
"count": "232",
"type": "4",
"canBeFiltered": true,
"stand": "1",
"ckeKeyWordBury": "eid=100^^tagid=4718ed7ec8af7f16^^pid=30006^^sku=1097069^^sversion=1000^^token=3655b7ed53a7de05"
}
,
{
"id": "d1a61b29c4d11818",
"name": "分量足够",
"rid": "d1a61b29c4d11818",
"count": "2",
"type": "4",
"canBeFiltered": true,
"stand": "1",
"ckeKeyWordBury": "eid=100^^tagid=d1a61b29c4d11818^^pid=30006^^sku=1097069^^sversion=1000^^token=95fdbedf7a09f343"
}
{
"id": "9737cdd8cafd7493",
"name": "不会粘手",
"rid": "9737cdd8cafd7493",
"count": "1",
"type": "4",
"canBeFiltered": true,
"stand": "1",
"ckeKeyWordBury": "eid=100^^tagid=9737cdd8cafd7493^^pid=30006^^sku=1097069^^sversion=1000^^token=bf26e154e8b8ccae"
}
,
{
"id": "63ad1a9d2a96b819",
"name": "安全环保",
"rid": "63ad1a9d2a96b819",
"count": "1",
"type": "4",
"canBeFiltered": true,
"stand": "1",
"ckeKeyWordBury": "eid=100^^tagid=63ad1a9d2a96b819^^pid=30006^^sku=1097069^^sversion=1000^^token=480bafbb3298af4c"
}
]
})'
comment_count = re.search(r'.*"CommentCount":\s?"?(\d+)"?,.*', text, re.DOTALL)
good_count = re.search(r'.*"GoodCount":\s?"?(\d+)"?,.*', text, re.DOTALL)
good_rate = re.search(r'.*"GoodRate": "(.*?)",.*', text, re.DOTALL)
或者
good_rate = re.search(r'"GoodRate":\s+?"([.\d]+?)"?,.*', text, re.DOTALL)