你好,我是悦创。
先修知识:str.find():>>> Python find()
"""project = 'Code', file_name = 'test', author = 'AI悦创'time = '/3/13 19:21', product_name = PyCharm# code is far away from bugs with the god animal protectingI love animals. They taste delicious."""import requestsimport jsonurl = '/comment/productCommentSummaries.action?referenceIds=29505771199&callback=jQuery7981360'html = requests.get(url).textstart_position = html.find('{"CommentsCount":')end_position = html.find('"PoorRateStyle":0}]}') + len('"PoorRateStyle":0}]}')result = html[start_position: end_position]res = json.loads(result)print(len(res['CommentsCount']))print(res['CommentsCount'][0]['DefaultGoodCountStr'])dict_data = res['CommentsCount'][0]result = dict_data.get('DefaultGoodCountStr', None)print(result)
运行结果:
110+10+
尝试第二个链接:
"""project = 'Code', file_name = 'test', author = 'AI悦创'time = '/3/13 19:21', product_name = PyCharm# code is far away from bugs with the god animal protectingI love animals. They taste delicious."""import requestsimport json# url = '/comment/productCommentSummaries.action?referenceIds=29505771199&callback=jQuery7981360'url = '/comment/productCommentSummaries.action?referenceIds=12353915&callback=jQuery7981360'html = requests.get(url).textstart_position = html.find('{"CommentsCount":')end_position = html.find('"PoorRateStyle":0}]}') + len('"PoorRateStyle":0}]}')result = html[start_position: end_position]res = json.loads(result)print(len(res['CommentsCount']))print(res['CommentsCount'][0]['DefaultGoodCountStr'])dict_data = res['CommentsCount'][0]result = dict_data.get('DefaultGoodCountStr', None)print(result)
运行结果:
112万+12万+
多个商品 id
import requestsimport json# 多个商品 idurl = '/comment/productCommentSummaries.action?referenceIds=12353915,11993134&callback=jQuery7981360'html = requests.get(url).textstart_position = html.find('{"CommentsCount":')end_position = html.find('"PoorRateStyle":0}]}') + len('"PoorRateStyle":0}]}')result = html[start_position: end_position]res = json.loads(result)# 第一个 idprint('第一个 id')print(len(res['CommentsCount']))print(res['CommentsCount'][0]['DefaultGoodCountStr'])dict_data = res['CommentsCount'][0]result = dict_data.get('DefaultGoodCountStr', None)print(result)# 第二个 idprint('第二个 id')print(len(res['CommentsCount']))print(res['CommentsCount'][1]['DefaultGoodCountStr'])dict_data = res['CommentsCount'][1]result = dict_data.get('DefaultGoodCountStr', None)print(result)
运行结果:
第一个 id212万+12万+第二个 id223万+23万+