def insert_jd_model_summary(model_summary: dict, comment: dict, jd_ss: Union[Shop, JDSku]): color, rom = parse_iPhone11_product_info(comment['productColor'], comment['productSize']) try: ms = ModelSummary.get( source='京东', is_self=jd_ss.is_self, color=color, rom=rom ) update_jd_summary_data(ms, model_summary) except ModelSummary.DoesNotExist: ModelSummary.create( source='京东', is_self=jd_ss.is_self, color=color, rom=rom, total=parse_jd_count_str(model_summary['commentCountStr']), good_rate=str(model_summary['goodRate'] * 100), default_good=parse_jd_count_str(model_summary['defaultGoodCountStr']), star_one=model_summary['score1Count'], star_two=model_summary['score2Count'], star_three=model_summary['score3Count'], star_four=model_summary['score4Count'], star_five=model_summary['score5Count'] )
def get_iPhone11_data_from_sn(browser: Chrome): # for sn_shop in Shop.select().where(Shop.source == '苏宁'): # print(f'------打开当前苏宁商品链接: {sn_shop.url}------') # browser.get(sn_shop.url) # # 获取所有SKU和评论统计 # get_sn_sku_and_comment_summary_from_api(browser, sn_shop) # # # 获取默认排序评论, 并遍历所有SKU # print('------开始获取默认排序评论------') # switch_to_sn_default_comments_page(browser, sn_shop.url) # get_sn_comments(browser, sn_shop) # 轮询各个SKU的商品页面 print('------SKU轮询开始------') for current_sku in SNSku.select(): print(f'------本轮SKU: {current_sku.sku}------') current_sku_url = 'https://product.suning.com/' + current_sku.shop_code + '/' + current_sku.sku + '.html' print(f'------正在打开当前SKU链接: {current_sku_url}------') browser.get(current_sku_url) print('------开始获取当前SKU默认排序评论------') switch_to_sn_sku_comments_page(browser, current_sku_url) get_sn_comments(browser, current_sku, sku_mode=True) current_sku.delete_instance() # 数据汇总后计算最终好评率 calculate_jd_and_sn_good_rate(CommentSummary.select().where(CommentSummary.source == '苏宁')) calculate_jd_and_sn_good_rate(ModelSummary.select().where(ModelSummary.source == '苏宁')) print('------苏宁平台数据获取完成------')
def insert_sn_model_summary(model_summary: dict, commodity_info: dict, sn_sku: SNSku): try: if commodity_info['charaterId1'] == '颜色': color, rom = parse_iPhone11_product_info(commodity_info['charaterDesc1'], commodity_info['charaterDesc2']) elif commodity_info['charaterId2'] == '颜色': color, rom = parse_iPhone11_product_info(commodity_info['charaterDesc2'], commodity_info['charaterDesc1']) else: color, rom = parse_iPhone11_product_info(commodity_info['charaterDesc1'], commodity_info['charaterDesc1']) except (AttributeError, KeyError): print('---输入产品信息不规范, 跳过此SKU评论统计信息---') return try: ms = ModelSummary.get( source='苏宁', is_self=sn_sku.is_self, color=color, rom=rom ) ms.total += model_summary['totalCount'] ms.default_good += model_summary['defaultCount'] ms.star_one += model_summary['oneStarCount'] ms.star_two += model_summary['twoStarCount'] ms.star_three += model_summary['threeStarCount'] ms.star_four += model_summary['fourStarCount'] ms.star_five += model_summary['fiveStarCount'] ms.save() except ModelSummary.DoesNotExist: ModelSummary.create( source='苏宁', is_self=sn_sku.is_self, color=color, rom=rom, total=model_summary['totalCount'], good_rate=str(model_summary['goodRate']), default_good=model_summary['defaultCount'], star_one=model_summary['oneStarCount'], star_two=model_summary['twoStarCount'], star_three=model_summary['threeStarCount'], star_four=model_summary['fourStarCount'], star_five=model_summary['fiveStarCount'], )
def get_jd_total(): jd_total = 0 jd_good_count = 0 jd_general_count = 0 jd_bad_count = 0 jd_cal_total = 0 jd_default_good = 0 jd_star_one = 0 jd_star_two = 0 jd_star_three = 0 jd_star_four = 0 jd_star_five = 0 for jd_comments_summary in CommentSummary.select().where( CommentSummary.source == '京东'): jd_total += jd_comments_summary.default_good + jd_comments_summary.star_one + jd_comments_summary.star_two + \ jd_comments_summary.star_three + jd_comments_summary.star_four + jd_comments_summary.star_five jd_good_count += jd_comments_summary.star_four + jd_comments_summary.star_five jd_general_count += jd_comments_summary.star_two + jd_comments_summary.star_three jd_bad_count += jd_comments_summary.star_one jd_cal_total += jd_comments_summary.star_one + jd_comments_summary.star_two + jd_comments_summary.star_three + \ jd_comments_summary.star_four + jd_comments_summary.star_five jd_default_good += jd_comments_summary.default_good jd_star_one += jd_comments_summary.star_one jd_star_two += jd_comments_summary.star_two jd_star_three += jd_comments_summary.star_three jd_star_four += jd_comments_summary.star_four jd_star_five += jd_comments_summary.star_five jd_good_rate = calculate_percentage(jd_cal_total, jd_good_count) jd_models_total = 0 for ms in ModelSummary.select().where(ModelSummary.source == '京东'): jd_models_total += ms.default_good + ms.star_one + ms.star_two + ms.star_three + ms.star_four + ms.star_five Total.create(source='京东', total=jd_total, all_models_total=jd_models_total, good_rate=jd_good_rate, default_good=jd_default_good, good_count=jd_good_count, general_count=jd_general_count, bad_count=jd_bad_count, star_one=jd_star_one, star_two=jd_star_two, star_three=jd_star_three, star_four=jd_star_four, star_five=jd_star_five)
def get_iPhone11_data_from_jd(browser: Chrome): for jd_shop in Shop.select().where(Shop.source == '京东'): print(f'------打开当前京东商品链接: {jd_shop.url}------') browser.get(jd_shop.url) # 打开商品页面 # 获取已上架SKU get_jd_sku_from_api(browser, jd_shop) # 获取默认推荐排序评论和默认时间排序评论, 并遍历所有SKU print('------开始获取默认推荐排序评论------') switch_to_jd_default_comments_page(browser, jd_shop.url) # 打开评论默认页面 get_jd_comments(browser, jd_shop, get_sku=True, summary=True) # 从全部评价标签获取评论和统计信息 print('------开始获取默认时间排序评论------') switch_to_jd_default_comments_page(browser, jd_shop.url) switch_to_jd_time_sort(browser) # 切换到时间排序 get_jd_comments(browser, jd_shop, get_sku=True) # 从全部评价标签获取评论 # 轮询各个SKU的商品页面 print('------SKU轮询开始------') for current_sku in JDSku.select(): try: print(f'------本轮SKU: {current_sku.sku}------') current_sku_url = 'https://item.jd.com/' + current_sku.sku + '.html' print(f'------正在打开当前SKU链接: {current_sku_url}------') browser.get(current_sku_url) print('------开始获取当前SKU推荐排序评论------') switch_to_jd_sku_comments_page(browser, current_sku_url) get_jd_comments(browser, current_sku, sku_mode=True, summary=True) # 从全部评价标签获取评论和统计信息 print('------开始获取当前SKU时间排序评论------') switch_to_jd_sku_comments_page(browser, current_sku_url) switch_to_jd_time_sort(browser) # 切换到时间排序 get_jd_comments(browser, current_sku, sku_mode=True) # 从全部评价标签获取评论 except JavascriptException: back_to_first_window(browser) print('---评论页面异常---') current_sku.delete_instance() # 数据汇总后计算最终好评率 calculate_jd_and_sn_good_rate(CommentSummary.select().where(CommentSummary.source == '京东')) calculate_jd_and_sn_good_rate(ModelSummary.select().where(ModelSummary.source == '京东')) print('------京东平台数据获取完成------')
def get_sn_total(): sn_good_count = 0 sn_general_count = 0 sn_bad_count = 0 sn_total = 0 sn_default_good = 0 sn_star_one = 0 sn_star_two = 0 sn_star_three = 0 sn_star_four = 0 sn_star_five = 0 for sn_comments_summary in CommentSummary.select().where( CommentSummary.source == '苏宁'): sn_good_count += sn_comments_summary.star_four + sn_comments_summary.star_five sn_general_count += sn_comments_summary.star_two + sn_comments_summary.star_three sn_bad_count += sn_comments_summary.star_one sn_total += sn_comments_summary.total sn_default_good += sn_comments_summary.default_good sn_star_one += sn_comments_summary.star_one sn_star_two += sn_comments_summary.star_two sn_star_three += sn_comments_summary.star_three sn_star_four += sn_comments_summary.star_four sn_star_five += sn_comments_summary.star_five sn_cal_total = sn_good_count + sn_general_count + sn_bad_count sn_good_rate = calculate_percentage(sn_cal_total, sn_good_count) sn_models_total = 0 for ms in ModelSummary.select().where(ModelSummary.source == '苏宁'): sn_models_total += ms.total Total.create(source='苏宁', total=sn_total, all_models_total=sn_models_total, good_rate=sn_good_rate, default_good=sn_default_good, good_count=sn_good_count, general_count=sn_general_count, bad_count=sn_bad_count, star_one=sn_star_one, star_two=sn_star_two, star_three=sn_star_three, star_four=sn_star_four, star_five=sn_star_five)
def get_model_count(): # 苏宁和京东数据合并 all_color = ['黑色', '白色', '红色', '黄色', '紫色', '绿色'] all_rom = ['64GB', '128GB', '256GB'] for color in all_color: for rom in all_rom: all_ms = ModelSummary.select().where((ModelSummary.color == color) & (ModelSummary.rom == rom)) mc = ModelCount.create(color=color, rom=rom) for ms in all_ms: if ms.source == '京东': mc.total += ms.default_good + ms.star_one + ms.star_two + \ ms.star_three + ms.star_four + ms.star_five mc.cal_total += ms.star_one + ms.star_two + ms.star_three + ms.star_four + ms.star_five mc.save() if ms.source == '苏宁': mc.total += ms.total mc.cal_total += ms.total mc.save() mc.default_good += ms.default_good mc.good_count += ms.star_four + ms.star_five mc.general_count += ms.star_two + ms.star_three mc.bad_count += ms.star_one mc.star_one += ms.star_one mc.star_two += ms.star_two mc.star_three += ms.star_three mc.star_four += ms.star_four mc.star_five += ms.star_five mc.save() # 计算占比和好评率 (京东计算规则) jd_total = Total.get(Total.source == '京东') sn_total = Total.get(Total.source == '苏宁') models_total = jd_total.all_models_total + sn_total.all_models_total for mc in ModelCount.select(): mc.percentage = calculate_percentage(models_total, mc.total) mc.good_rate = calculate_percentage(mc.cal_total, mc.good_count) mc.save()