preface

So today we will 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 modules that come with Python.

Environment set up

Install Python and add it to the environment variables. PIP installs the required modules.

See how DecryptLogin is installed (since it is frequently updated, please make sure that you update the DecryptLogin, otherwise it may cause errors in new cases).

Introduction of the principle

First of all, open netease Cloud, find a random user to refresh his listening charts page and capture packets:

It turns out that you can get a list of the user’s songs by asking for a link like this:

https://music163..com/weapi/v1/play/record?csrf_token=90f3b95109118c7335b896e2c24d7ebd
Copy the code

The data returned looks like this:

Now consider how the request is constructed. First, where does the cSRF_token come from? As mentioned in the previous article, this parameter can be found directly in the cookies after the user logs in, as shown in the red box below. The cSRF_token is optional.

Now let’s think about how to calculate the POST parameter because the DecryptLogin library is already written, so let’s just think about how to get the original post data. If we look at the initiator, we can see that the Ajax request is initiated by a core.js file:

To find this function, search for encSecKey globally and analyze its calculation process to find it. Then run the script again. We can see the original post data:

So we can happily start writing code. The core code looks like this:

Obtain a user's music charts
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
Copy the code

Follow 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 profile or private message to obtain related files.