Don’t know if you have ever played no small program of science and technology award assistant can not played in portal search lottery WeChat small program assistant, home have a lot of prizes for lucky draw, after I found a few days ago, I put there all the prizes of point a, grows a sudden think, can use python to realize automatic sweepstakes? So I don’t have to check in every day, I just need to care if I win. The answer is yes, today we will bring you how to achieve automatic lottery, free hands.

Requires knowledge

  • Will use Charles to grab the phone

  • With requests

Well, that’s all it takes. If you will not capture the package, please baidu, here is not a spread out said.

1. Analyze the page

We first open Charles to prepare, and then open the home page of the small program lottery Assistant. We can see two columns of daily benefits and self-help benefits, and there is a button to load more at the bottom of self-help benefits, which are all the simulated requests we need to make.

Now you can look at the data captured in Charles, and it is easy to find that the following requests are one by one corresponding to the data in the picture above.

Get the daily welfare prize data request url for https://lucky.nocode.com/public_lottery?page=1&size=5, And get the self-help welfare prize data request url for https://lucky.nocode.com/square, click on the load more request url for https://lucky.nocode.com/square?cursor=355&start=447, All three urls are requested by get, so it can be said that the URL will not change. It should be noted that when we add the request header, we need to add the authorization parameter, which translates as authorization, in fact, is used to verify the identity, so to speak, we can log in wechat by adding this parameter.

The next step is to see how the request is made after clicking the lucky draw button.

Let’s first click on the daily bonus draw, and you can see the request at Charles

Id/join request url for https://lucky.nocode.com/lottery/ prize, url and change, there is the prize of id, the request is to post, the from – data inside the data according to the experience to know it is a 13 timestamp. Now that we’re done, let’s see what we can do about self-help.

As you can see, the requests are all the same, so I won’t go into that.

2. Code implementation

This is the realization of getting daily benefits

def __get_public_draw(self):

       url = 'https://lucky.nocode.com/public_lottery?page=1&size=5'

       response = requests.get(url, headers=self.headers, verify=False)

       if response.status_code == 200:

           for prize in response.json()['data'] :

               if not prize['joined'and 'not satisfied' not in prize.get('condition_error'.'1') :

                   yield prize['id'], prize['prizes'] ['data'] [0] ['name']



       else:

           print('Request failed with status code %s' % response.status_code)Copy the code

You can slide it left and right

If the certificate is not verified, an error will be reported. If the certificate is not verified, an error will be reported

OpenSSL.SSL.Error: [('SSL routines', 'ssl3_get_server_certificate', 'certificate verify failed')]

Certificate verification failed

Then comes the code implementation of self-help welfare

def __get_welfare(self):

       urls = ['https://lucky.nocode.com/square'.'https://lucky.nocode.com/square?cursor=293&start=394']

       for url in urls:

           response = requests.get(url, headers=self.headers, verify=False)

           if response.status_code == 200:

               for prize in response.json()['data'] :

                   if not prize['joined'] :

                       yield prize['id'], prize['initiator'] ['nick_name']Copy the code

There are two urls, and the parameters in the second URL can be set to fixed, but the rest is not to be discussed.

Finally, there is the implementation of automatic lottery

def __draw_prize(self, prize):

       print('Prizes for %s are being raffled' % prize[1])

       url = 'https://lucky.nocode.com/lottery/%s/join' % prize[0]

       data = {

           'form_id': int(time.time()*1000)

       }

       response = requests.post(url, data=data, headers=self.headers, verify=False).json()

       if response.get('data'.0) :

           print('Lucky draw')

       else:

           print('Lottery failed')Copy the code

Because time.time() results in a float value with a ten-digit integer, you need to multiply it by 1000 before taking the integer. Everything else is fine.

The last


This code you can run once a day, so that you can draw all the basic parameters of the prize, winning or not is another matter, I always do this anyway



I wish you the best of luck.

Ps: If the article is useful to you, why not use your little hands to like and forward it?

Recommended reading:

Python uses asynchrony to download a large hd image every second, fast?

Crawlers too slow? Try an asynchronous coroutine to speed things up!


Daily learning python

Code is not just buggy, it’s beautiful and fun