Preface:

It has been three weeks since we met. Recently, we have been working on the Python simulated login topic. Without further ado, let’s happily start to realize the simulated login and realize the netease cloud automatic check-in

The development tools

**Python**** version: **3.6.4

Related modules:

DecryptLogin module;

The argparse module;

And some modules that come with Python.

Environment set up

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

Introduction of the principle

Since you want to sign in, first of all, it is natural to simulate login. Here we will simply use our open source DecryptLogin library to implement the simulated login of netease Cloud Music:

@staticMethod def login(username, password): lg = login.Login() _, session = lg.music163(username, password) return sessionCopy the code

After successful login, we will analyze how to realize the automatic check-in of netease cloud music. Press F12 to open developer tools or right mouse button for detection, and then click the Netease Cloud Music check-in button:

You can find a post request that looks like a check-in request:

Its link composition is:

'https://music.163.com/weapi/point/dailyTask?csrf_token=' + csrf
Copy the code

As mentioned in the previous article, the CSRF parameter can be found in the cookies after login, like this:

csrf = re.findall('__csrf=(.*?) for', str(session.cookies))[0]
Copy the code

So now we have to solve the problem of how to find the original text of the data submitted by the POST request, because the content seen in the web page is encrypted, like this:

The first one is obviously where we found the check-in interface, so let’s click on the second one:

Query can’t be the text we want. If you try to succeed, won’t you make money? Call the DecryptLogin encryption algorithm for netease Cloud Music POST parameters, which is written in the DecryptLogin library, to encrypt data(all data carried by netease Cloud Music POST requests are encrypted with an encryption algorithm before submission) :

The from DecryptLogin. Platforms. Music163 import Cracker Cracker = Cracker (#) note: Data = {'type': typeID} data = cracker.get(data)Copy the code

Then send the request to test it:

signin_url = 'https://music.163.com/weapi/point/dailyTask?csrf_token=' + csrf headers = { 'User-Agent': 'the Mozilla / 5.0 (Windows NT 10.0; Win64; X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36', 'content-type ': 'application/x-www-form-urlencoded', 'Referer': 'http://music.163.com/discover', 'Accept': '*/*' } res = self.session.post(signin_url, headers=headers, data=data)Copy the code

The returned data looks like this:

This result shows that our guess is exactly right, only because we have already checked in, so it shows double check-ins. Finally, to save the time of entering the account password at the command line each day, we can add a few lines of code at the top of the script:

if os.path.exists('config.json'):
  f = open('config.json', 'r', encoding='utf-8')
  info = json.load(f)
  f.close()
else:
  args = parseArgs()
  info = {'username': args.username, 'password': args.password}
  f = open('config.json', 'w', encoding='utf-8')
  json.dump(info, f)
  f.close()
Copy the code

If you have config.json in your current folder:

So we read the user’s account name and password directly from the JSON file, otherwise we start the command-line parameter parsing function

"Def parseArgs(): Parser = argparse.ArgumentParser(description=' automatic check in ') parser. Add_argument ('--username', dest='username', Help =' username ', type= STR, required=True) parser. Add_argument ('--password', dest='password', help=' password', type= STR, required=True) args = parser.parse_args() return argsCopy the code

Let the user enter the password manually and save it to the config.json file so that you don’t have to re-enter the password next time.

Results show

Operation mode:

Python signin.py –username username –password password

That’s the end of this article. Thank you for watching. Follow me every day in my Python simulation login series.

To thank you readers, I’d like to share some of my recent programming favorites to give back to each and every one of you in the hope that they can help you.

Dry goods mainly include:

  • More than 2000 Python e-books (both mainstream and classic books should be available)

    ③ project source code (forty or fifty interesting and classic practice projects and source code)

    ④Python basic introduction, crawler, Web development, big data analysis video (suitable for small white learning)

    ⑤ A Roadmap for Learning Python

**All done~ Complete source code + dry add Python beginner learning exchange group: **739021630