用Python分析了20万场吃鸡数据

首先,神枪镇楼

创新互联是一家专业提供龙沙企业网站建设,专注与网站设计制作、成都网站设计成都h5网站建设、小程序制作等业务。10年已为龙沙众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。

背景

最近老板爱上了吃鸡(手游:全军出击),经常拉着我们开黑,只能放弃午休的时间,陪老板在沙漠里奔波。 上周在在微信游戏频道看战绩的时候突发奇想,是不是可以通过这个方式抓取到很多战斗数据,然后分析看看有什么规律。

秀一波战绩,开黑情况下我们团队吃鸡率非常高,近100场吃鸡次数51次

简单评估了一下,觉得可行,咱就开始。

Step 1 分析数据接口

第一步当然是把这些战绩数据采集下来,首先我们需要了解页面背后的故事。去看看页面是如何获取战斗数据的。

使用Charles抓包

抓包实现

在Mac下推荐使用工具Charles来从协议层抓取手机上的流量,原理就是在Mac上开启一个代*理*服务器,然后将手机的网络代*理设置为Mac,这样手机上的所有流量都会经过我们的代*理*服务器了。 大致流程如下:

https加密流量的处理

在实际操作的时候发现微信所有的流量都走了HTTPS,导致我们的抓到的都是加密数据,对我们没有任何参考意义。 经过研究,可以通过在手机和电脑都安装Charles根证书的方式来实现对Https流量的分析,具体操作可以参考:

  • charles mac下https抓包和iphone https抓包
  • 解决Charles无法正常抓包iOS 11中的Https请求

安装证书后,我们的流量大致是这样子的

经过上述的配置,我们已经可以读取到https的请求和响应数据了,如下图所示。

  • windows下用findler可以实现相同的功能
  • 其实这就是一个非常典型的中间人场景

数据接口

接下来就根据这些数据来找出我们需要的接口了,经过分析,主要涉及三个接口

  • 获取用户信息接口
  • 获取用户战绩列表接口
  • 获取用户指定战绩详细信息接口

下面我们一个一个看

1. 获取用户信息接口

  • request
API /cgi-bin/gamewap/getpubgmdatacenterindex
方法 GET
参数 openid、pass_ticket
cookie key pass_ticket、uin、pgv_pvid、sd_cookie_crttime、sd_userid
  • response
 
 
 
  1.     "user_info": { 
  2.         "openid": "oODfo0pjBQkcNuR4XLTQ321xFVws", 
  3.         "head_img_url": "http://wx.qlogo.cn/mmhead/Q3auHgzwzM5hSWxxxxxUQPwW9ibxxxx9DlxLTsKWk97oWpDI0rg/96", 
  4.         "nick_name": "望", 
  5.         "role_name": "xxxx", 
  6.         "zone_area_id": 0, 
  7.         "plat_id": 1 
  8.     }, 
  9.     "battle_info": { 
  10.         "total_1": 75, 
  11.         "total_10": 336, 
  12.         "total_game": 745, 
  13.         "total_kill": 1669 
  14.     }, 
  15.     "battle_list": [{ 
  16.         "map_id": 1, 
  17.         "room_id": "6575389198189071197", 
  18.         "team_id": 57, 
  19.         "dt_event_time": 1530953799, 
  20.         "rank_in_ds": 3, 
  21.         "times_kill": 1, 
  22.         "label": "前五", 
  23.         "team_type": 1, 
  24.         "award_gold": 677, 
  25.         "mode": 0 
  26.     }], 
  27.     "appitem": { 
  28.         "AppID": "wx13051697527efc45", 
  29.         "IconURL": "https://mmocgame.qpic.cn/wechatgame/mEMdfrX5RU0dZFfNEdCsMJpfsof1HE0TP3cfZiboX0ZPxqh5aZnHjxPFXUGgsXmibe/0", 
  30.         "Name": "绝地求生 全军出击", 
  31.         "BriefName": "绝地求生 全军出击", 
  32.         "Desc": "官方正版绝地求生手游", 
  33.         "Brief": "枪战 | 808.2M", 
  34.         "WebURL": "https://game.weixin.qq.com/cgi-bin/h5/static/detail_v2/index.html?wechat_pkgid=detail_v2&appid=wx13051697527efc45&show_bubble=0", 
  35.         "DownloadInfo": { 
  36.             "DownloadURL": "https://itunes.apple.com/cn/app/id1304987143", 
  37.             "DownloadFlag": 5 
  38.         }, 
  39.         "Status": 0, 
  40.         "AppInfoFlag": 45, 
  41.         "Label": [], 
  42.         "AppStorePopUpDialogConfig": { 
  43.             "Duration": 1500, 
  44.             "Interval": 172800, 
  45.             "ServerTimestamp": 1531066098 
  46.         }, 
  47.         "HasEnabledChatGroup": false, 
  48.         "AppType": 0, 
  49.         "game_tag_list": ["绝地求生", "正版还原", "好友开黑", "百人对战", "超大地图"], 
  50.         "recommend_reason": "正版绝地求生,荒野射击", 
  51.         "size_desc": "808.2M" 
  52.     }, 
  53.     "is_guest": true, 
  54.     "is_blocked": false, 
  55.     "errcode": 0, 
  56.     "errmsg": "ok" 
  • 分析

openid是用户的惟一标识。

2. 获取用户战绩列表接口

  • request
API /cgi-bin/gamewap/getpubgmbattlelist
方法 GET
参数 openid、pass_ticket、plat_id、after_time、limit
cookie key pass_ticket、uin、pgv_pvid、sd_cookie_crttime、sd_userid
  • response
 
 
 
  1. "errcode": 0, 
  2. "errmsg": "ok", 
  3. "next_after_time": 1528120556, 
  4. "battle_list": [{ 
  5.     "map_id": 1, 
  6.     "room_id": "6575389198111172597", 
  7.     "team_id": 57, 
  8.     "dt_event_time": 1530953799, 
  9.     "rank_in_ds": 3, 
  10.     "times_kill": 1, 
  11.     "label": "前五", 
  12.     "team_type": 1, 
  13.     "award_gold": 677, 
  14.     "mode": 0 
  15. }, { 
  16.     "map_id": 1, 
  17.     "room_id": "6575336498940384115", 
  18.     "team_id": 11, 
  19.     "dt_event_time": 1530941404, 
  20.     "rank_in_ds": 5, 
  21.     "times_kill": 2, 
  22.     "label": "前五", 
  23.     "team_type": 1, 
  24.     "award_gold": 632, 
  25.     "mode": 0 
  26. }], 
  27. "has_next": true 
  28. }
  • 分析

这个接口用after_time来进行分页,遍历获取时可以根据接口响应的has_next和next_after_time来判断是否还有下一页的数据。

列表里面的room_id是每一场battle的惟一标识。

3. 获取用户战绩详情接口

  • request
API /cgi-bin/gamewap/getpubgmbattledetail
方法 GET
参数 openid、pass_ticket、room_id
cookie key pass_ticket、uin、pgv_pvid、sd_cookie_crttime、sd_userid
  • request
 
 
 
  1. "errcode": 0, 
  2. "errmsg": "ok", 
  3. "base_info": { 
  4.     "nick_name": "柚茶", 
  5.     "head_img_url": "http://wx.qlogo.cn/mmhead/xxxx/96", 
  6.     "dt_event_time": 1528648165, 
  7.     "team_type": 4, 
  8.     "rank": 1, 
  9.     "player_count": 100, 
  10.     "role_sex": 1, 
  11.     "label": "大吉大利", 
  12.     "openid": "oODfo0s1w5lWjmxxxxxgQkcCljXQ" 
  13. }, 
  14. "battle_info": { 
  15.     "award_gold": 622, 
  16.     "times_kill": 6, 
  17.     "times_head_shot": 0, 
  18.     "damage": 537, 
  19.     "times_assist": 3, 
  20.     "survival_duration": 1629, 
  21.     "times_save": 0, 
  22.     "times_reborn": 0, 
  23.     "vehicle_kill": 1, 
  24.     "forward_distance": 10140, 
  25.     "driving_distance": 5934, 
  26.     "dead_poison_circle_no": 6, 
  27.     "top_kill_distance": 223, 
  28.     "top_kill_distance_weapon_use": 2924130819, 
  29.     "be_kill_user": { 
  30.         "nick_name": "小旭", 
  31.         "head_img_url": "http://wx.qlogo.cn/mmhead/ibLButGMnqJNFsUtStNEV8tzlH1QpwPiaF9kxxxxx66G3ibjic6Ng2Rcg/96", 
  32.         "weapon_use": 20101000001, 
  33.         "openid": "oODfo0qrPLExxxxc0QKjFPnPxyI" 
  34.     }, 
  35.     "label": "大吉大利" 
  36. }, 
  37. "team_info": { 
  38.     "user_list": [{ 
  39.         "nick_name": "ooo", 
  40.         "times_kill": 6, 
  41.         "assist_count": 3, 
  42.         "survival_duration": 1638, 
  43.         "award_gold": 632, 
  44.         "head_img_url": "http://wx.qlogo.cn/mmhead/Q3auHgzwzM4k4RXdyxavNxxxxUjcX6Tl47MNNV1dZDliazRKRg", 
  45.         "openid": "oODfo0xxxxf1bRAXE-q-lEezK0k" 
  46.     }, { 
  47.         "nick_name": "我吃炒肉", 
  48.         "times_kill": 2, 
  49.         "assist_count": 2, 
  50.         "survival_duration": 1502, 
  51.         "award_gold": 583,
  52.         "head_img_url": "http://wx.qlogo.cn/mmhead/sTJptKvBQLKd5SAAjOF0VrwiapUxxxxFffxoDUcrVjYbDf9pNENQ", 
  53.         "openid": "oODfo0gIyDxxxxZpUrSrpapZSDT0" 
  54.     }] 
  55. }, 
  56. "is_guest": true, 
  57. "is_blocked": false 
  58. }
  • 分析
  • 这个接口响应了战斗的详细信息,包括杀*敌数、爆*头数、救人数、跑动距离等等,足够我们分析了。
  • 这个接口还响应了是被谁杀死的以及组团成员的openid,利用这个特性我们这可无限深度的发散爬取更多用户的数据。

至于cookie中的息pass_ticket等信息肯定是用于权限认证的,在上述的几次请求中这些信息都没有变化,所以我们不需要深研其是怎么算出来的,只需要抓包提取到默认信息后填到代码里面就可以用了。

Step 2 爬取数据

接口已经确定下来了,接下来就是去抓取足够量的数据了。

使用requests请求接口获取数据

 
 
 
  1. url = 'https://game.weixin.qq.com/cgi-bin/gamewap/getpubgmdatacenterindex?openid=%s&plat_id=0&uin=&key=&pass_ticket=%s' % (openid, settings.pass_ticket) 
  2.     r = requests.get(url=url, cookies=settings.def_cookies, headers=settings.def_headers, timeout=(5.0, 5.0)) 
  3.     tmp = r.json() 
  4.     wfile = os.path.join(settings.Res_UserInfo_Dir, '%s.txt' % (rediskeys.user(openid)))  
  5.     with codecs.open(wfile, 'w', 'utf-8') as wf: 
  6.         wf.write(simplejson.dumps(tmp, indent=2, sort_keys=True, ensure_ascii=False))

参照这种方式我们可以很快把另外两个接口写好。

使用redis来标记已经爬取过的信息

在上述接口中我们可能从用户A的入口进去找到用户B的openid,然后从用户B的入口进去又找到用户A的openid,为了避免重复采集,所以我们需要记录下哪些信息是我们采集过的。 核心代码片断:

 
 
 
  1. # rediskeys.user_battle_list 根据openid获取存在redis中的key值 
  2. def user_battle_list(openid): 
  3.     return 'ubl_%s' % (openid) 
  4. # 在提取battle list之前,首先判断这用用户的数据是否已经提取过了 
  5. if settings.DataRedis.get(rediskeys.user_battle_list(openid)): 
  6.         return True 
  7. # 在提取battle list之后,需要在redis中记录用户信息 
  8. settings.DataRedis.set(rediskeys.user_battle_list(openid), 1)

使用celery来管理队列

celery是一个非常好用的分布式队列管理工具,我这次只打算在我自己的电脑上运行,所以并没有用到分布式的功能。 我们创建三个task和三个queue

 
 
 
  1. task_queues = ( 
  2.     Queue('queue_get_battle_info', exchange=Exchange('priority', type='direct'), routing_key='gbi'), 
  3.     Queue('queue_get_battle_list', exchange=Exchange('priority', type='direct'), routing_key='gbl'),
  4.     Queue('queue_get_user_info', exchange=Exchange('priority', type='direct'), routing_key='gui'), 
  5. )   
  6. task_routes = ([ 
  7.     ('get_battle_info', {'queue': 'queue_get_battle_info'}), 
  8.     ('get_battle_list', {'queue': 'queue_get_battle_list'}), 
  9.     ('get_user_info', {'queue': 'queue_get_user_info'}), 
  10. ],)

然后在task中控制API请求和Redis数据实现完整的任务逻辑,如:

 
 
 
  1. @app.task(name='get_battle_list') 
  2. def get_battle_list(openid, plat_id=None, after_time=0, update_time=None): 
  3.     # 判断是否已经取过用户战绩列表信息
  4.     if settings.DataRedis.get(rediskeys.user_battle_list(openid)): 
  5.         return True  
  6.     if not plat_id: 
  7.         try: 
  8.             # 提取用户信息 
  9.             us = handles.get_user_info_handles(openid) 
  10.             plat_id=us['plat_id'] 
  11.         except Exception as e: 
  12.             print 'can not get user plat_id', openid, traceback.format_exc() 
  13.             return False 
  14.     # 提取战绩列表 
  15.     battle_list = handles.get_battle_list_handle(openid, plat_id, after_time=0, update_time=None)     
  16.     # 为每一场战斗创建异步获取详情任务 
  17.     for room_id in battle_list: 
  18.         if not settings.DataRedis.get(rediskeys.user_battle(openid, room_id)): 
  19.             get_battle_info.delay(openid, plat_id, room_id)
  20.     return True

开始抓取

因为我们是发散是爬虫,所以需要给代码一个用户的入口,所以需要手动创建一个用户的采集任务

 
 
 
  1. from tasks.all import get_battle_list   
  2. my_openid = 'oODfo0oIErZI2xxx9xPlVyQbRPgY' 
  3. my_platid = '0'   
  4. get_battle_list.delay(my_openid, my_platid, after_time=0, update_time=None)

有入口之后我们就用celery来启动worker去开始爬虫

 
 
 
  1. # 启动获取用户详情worker 
  2. celery -A tasks.all worker -c 5 --queue=queue_get_user_info --loglevel=info -n get_user_info@%h  
  3. # 启动获取战绩列表worker 
  4. celery -A tasks.all worker -c 5 --queue=queue_get_battle_list --loglevel=info -n get_battle_list@%h   
  5. # 启动获取战绩详情worker 
  6. celery -A tasks.all worker -c 30 --queue=queue_get_battle_info --loglevel=info -n get_battle_info@%h

这样我们的爬虫就可以愉快的跑起来了。再通过celery-flower来查看执行情况。

 
 
 
  1. celery flower -A tasks.all --broker=redis://:$REDIS_PASS@$REDIS_HOST:$REDIS_PORT/10

通过flower,我们可以看到运行的效率还是非常不错的。

在执行过程中会发现get_battle_list跑太快,导致get_battle_info即使开了30个并发都还会积压很多,所以需要适时的去停一下这些worker。 在我们抓到20万条信息之后就可以停下来了。

Step 3 数据分析

分析方案

20万场战斗的数据已经抓取好了,全部分成json文件存在我本地磁盘上,接下来就做一些简单的分析。 python在数据分析领域也非常强大,有很多非常优秀的库,如pandas和NumPy,可惜我都没有学过,而且对于一个高考数学只考了70几分的人来说,数据分析实在是难,所以就自己写了一个非常简单的程序来做一些浅度分析。 需要进行深度分析,又不想自己爬虫的大牛可以联系我打包这些数据。

 
 
 
  1. # coding=utf-8 
  2. import os
  3. import json 
  4. import datetime 
  5. import math  
  6. from conf import settings   
  7. class UserTeamTypeData: 
  8.     def __init__(self, team_type, player_count): 
  9.         self.team_type = team_type
  10.         self.player_count = player_count 
  11.         self.label = {} 
  12.         self.dead_poison_circle_no = {} 
  13.         self.count = 0 
  14.         self.damage = 0 
  15.         self.survival_duration = 0  # 生存时间 
  16.         self.driving_distance = 0 
  17.         self.forward_distance = 0 
  18.         self.times_assist = 0  # 助攻 
  19.         self.times_head_shot = 0 
  20.         self.times_kill = 0 
  21.         self.times_reborn = 0  # 被救次数 
  22.         self.times_save = 0  # 救人次数 
  23.         self.top_kill_distance = [] 
  24.         self.top_kill_distance_weapon_use = {} 
  25.         self.vehicle_kill = 0  # 车辆杀死 
  26.         self.award_gold = 0 
  27.         self.times_reborn_by_role_sex = {0: 0, 1: 0}  # 0 男 1 女 
  28.         self.times_save_by_role_sex = {0: 0, 1: 0}  # 0 男 1 女  
  29.     def update_dead_poison_circle_no(self, dead_poison_circle_no): 
  30.         if dead_poison_circle_no in self.dead_poison_circle_no: 
  31.             self.dead_poison_circle_no[dead_poison_circle_no] += 1 
  32.         else: 
  33.             self.dead_poison_circle_no[dead_poison_circle_no] = 1  
  34.     def update_times_reborn_and_save_by_role_sex(self, role, times_reborn, times_save): 
  35.         if role not in self.times_reborn_by_role_sex: 
  36.             return  
  37.         self.times_reborn_by_role_sex[role] += times_reborn 
  38.         self.times_save_by_role_sex[role] += times_save  
  39.     def update_top_kill_distance_weapon_use(self, weaponid): 
  40.         if weaponid not in self.top_kill_distance_weapon_use: 
  41.             self.top_kill_distance_weapon_use[weaponid] = 1 
  42.         else: 
  43.             self.top_kill_distance_weapon_use[weaponid] += 1 
  44.  
  45. class UserBattleData:  
  46.     def __init__(self, openid): 
  47.         self.openid = openid 
  48.         self.team_type_res = {} 
  49.         self.label = {} 
  50.         self.hour_counter = {} 
  51.         self.weekday_counter = {} 
  52.         self.usetime = 0 
  53.         self.day_record = set() 
  54.         self.battle_counter = 0  
  55.     def get_avg_use_time_per_day(self): 
  56.         # print "get_avg_use_time_per_day:", self.openid, self.usetime, len(self.day_record), self.usetime / len(self.day_record) 
  57.         return self.usetime / len(self.day_record)  
  58.     def update_label(self, lable): 
  59.         if lable in self.label: 
  60.             self.label[lable] += 1
  61.          else: 
  62.             self.label[lable] = 1  
  63.     def get_team_type_data(self, team_type, player_count): 
  64.         player_count = int(math.ceil(float(player_count) / 10)) 
  65.         team_type_key = '%d_%d' % (team_type, player_count)  
  66.         if team_type_key not in self.team_type_res: 
  67.             userteamtypedata = UserTeamTypeData(team_type, player_count) 
  68.             self.team_type_res[team_type_key] = userteamtypedata 
  69.         else: 
  70.             userteamtypedata = self.team_type_res[team_type_key]  
  71.         return userteamtypedata  
  72.     def update_user_time_property(self, dt_event_time): 
  73.         dt_event_time = datetime.datetime.fromtimestamp(dt_event_time) 
  74.         hour = dt_event_time.hour 
  75.         if hour in self.hour_counter: 
  76.             self.hour_counter[hour] += 1 
  77.         else: 
  78.             self.hour_counter[hour] = 1  
  79.         weekday = dt_event_time.weekday() 
  80.         if weekday in self.weekday_counter: 
  81.             self.weekday_counter[weekday] += 1 
  82.         else: 
  83.             self.weekday_counter[weekday] = 1  
  84.         self.day_record.add(dt_event_time.date()) 
  85.  
  86.     def update_battle_info_by_room(self, roomid): 
  87.         # print '  load ', self.openid, roomid 
  88.         file = os.path.join(settings.Res_UserBattleInfo_Dir, self.openid, '%s.txt' % roomid)  
  89.         with open(file, 'r') as rf: 
  90.             battledata = json.load(rf)  
  91.         self.battle_counter += 1 
  92.         base_info = battledata['base_info'] 
  93.         self.update_user_time_property(base_info['dt_event_time']) 
  94.         battle_info = battledata['battle_info']  
  95.         userteamtypedata = self.get_team_type_data(base_info['team_type'], base_info['player_count']) 
  96.         userteamtypedata.count += 1 
  97.         userteamtypedata.award_gold += battle_info['award_gold'] 
  98.         userteamtypedata.damage += battle_info['damage'] 
  99.         userteamtypedata.update_dead_poison_circle_no(battle_info['dead_poison_circle_no']) 
  100.         userteamtypedata.driving_distance += battle_info['driving_distance'] 
  101.         userteamtypedata.forward_distance += battle_info['forward_distance'] 
  102.         self.update_label(battle_info['label']) 
  103.         userteamtypedata.survival_duration += battle_info['survival_duration'] 
  104.         self.usetime += battle_info['survival_duration']/60 
  105.         userteamtypedata.times_assist += battle_info['times_assist']
  106.         userteamtypedata.times_head_shot += battle_info['times_head_shot']  当前名称:用Python分析了20万场吃鸡数据
    标题路径:http://www.shufengxianlan.com/qtweb/news36/495236.html

    网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

    广告

    声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联