preface

So today we are going to climb a wave of netease cloud personal music charts. Let’s get started happily

The development tools

Python version: 3.6.4
Related modules:

The argparse module;

DecryptLogin module;

Prettytable module;

And some of the modules that come with Python.

Environment set up

Install Python and add it to the environment variable, and the PIP will install the appropriate module.

Please refer to the installation method of DecryptLogin (since it is often updated, if you have already installed it, please remember to update it, otherwise it may report an error in a new case).

Introduction of the principle

First, open up the netease cloud, ask a random user to refresh his/her listeningcharts page and grab the package:

You can get the user’s listening charts by simply requesting a link like this:

https://music.163.com/weapi/v1/play/record?csrf_token=90f3b95109118c7335b896e2c24d7ebd

The returned data looks like this:

The csrf_token parameter can be directly found in the user’s logged in cookies, as shown in the red box below. (Of course, if you can also see the target user’s listening charts when you are not logged in, you can also see the user’s listening charts.) So the csrf_token is actually dispensable) :

Next, consider how to calculate the POST parameter. Since the DecryPtLogin library is already written, we will only consider how to get the original POST data. If we look at the initiator, we find that the initiator of the Ajax request is a core.js file:

Click in and put a breakpoint in the corresponding position (that is, the function that needs to use the original text of the POST data to calculate the final POST parameter), then rerun the script. We can see the original text of the POST data:

So we can happily start writing code. The core code is as follows:

Def getLeaderboard(self, uid): url = 'https://music.163.com/weapi/v1/play/record?csrf_token=' + self.csrf data = { 'type': '-1', 'uid': uid, 'limit': '1000', 'offset': '0', 'total': 'true', 'csrf_token': self.csrf } res = self.session.post(url, headers=self.headers, data=self.cracker.get(data)) res_json = res.json() leader_board = {'weekData': [], 'allData': []} if res_json['code'] == 200: all_data = res_json.get('allData') for item in all_data: songname = item.get('song').get('name') songid = item.get('song').get('id') play_count = item.get('playCount') leader_board['allData'].append([songid, songname, play_count]) week_data = res_json.get('weekData') for item in week_data: songname = item.get('song').get('name') songid = item.get('song').get('id') play_count = item.get('playCount') leader_board['weekData'].append([songid, songname, play_count]) else: raise RuntimeError('Fail to get leaderboard for %s... ' % uid) return leader_board

After reading this article like friends point a like support, pay attention to me every day to share Python simulation login series, the next article to share the hook network simulation login

All done~ complete source code see personal profile or private message access to related files.