Master this technology, the world no longer difficult to reply to the post

I believe that many students will encounter problems when browsing the forum: some posts are marked “reply to view more”; Or some forums link the number of replies to the level of the account (for example, cao Liu). Personally, it’s a pain in the ass. First, you reply a post, need to open forum, open a post again, and then find the position of reply, input to reply the content, and then click reply, although a SAO operation, but cost time and energy. Secondly, there is a time interval between some reply posts, so it is impossible to upgrade the account quickly and have to wait a few seconds to dozens of minutes.

So, again, I thought of Python, which is a very useful tool to have fun with. Yes, Python is a very useful tool in my opinion.

Here, I mainly for 1024 website reply to do actual combat treatment, the reasons are as follows:

  1. 1024 account is divided into grades, in the “novice” area account, a lot of restrictions, if you want to upgrade to “chivalry”, the way only post, or reply to others.
  2. There is a time interval for novice replies. 1024 seconds, about 17 minutes. And you can only respond to 10 posts a day.
  3. Bingzhe upgrade account principle, but also earn U, and not for fun.

Well, we’re going to write a Python program that accomplishes several of our purposes:

  1. Ability to automatically reply to posts.
  2. Record the reply result and check whether the reply is successful.
  3. It would be nice to have something like a timer that can run again after 1024 seconds.

OK, let’s break down our problem briefly: a module that executes at a certain time; An auto-reply module. In the automatic reply module, we log in to the website through the account name and password, and then select the reply posts and perform the reply operation.

So, let’s start with the second autoreply module. Step one, let’s do the login function.

The login

Python emulated user login forum. This is a very common task. You Google it, you get a bunch of stuff. What we mainly use here is to capture packets, analyze HTTP POST parameters, and then simulate an HTTP POST request by Python to achieve login.

The first step is to capture the bag

There are many kinds of tools to capture packets. Some of them are easy to use, such as Filter for Windows and Charles for Mac. These are just basic functions. The most powerful is wireshark. I will choose Charles to do the processing here. As for the configuration and use of Charles, please refer to this article.

Login, first we come to the login interface:

Looking at the screen just like any other login page, we enter our account password, click the “Submit” button, and we send an HTTP POST request. It can be caught in Charles. Here we are mainly concerned with two points:

  • HTTP post Header
  • HTTP post Form

Because these are the two that we need to construct ourselves later.

Here’s what we caught:

This way, we can construct the login data in the same way.

Second, construct the data

In this case, we need to use the request.session() method to make the request. In the method passing in parameters, we have the HTTP headers and form data that we constructed ourselves. So we can construct a picture that looks like this by using the parameters in the figure above:

login_values = {
        "pwuser": "XXXXX"."pwpwd": "XXXXX"."hideid": "0"."forward": "http://dc.itbb.men/thread0806.php? fid=7"."jumpurl": "http://dc.itbb.men/thread0806.php? fid=7"."step": "2"
    }

    login_header = {
        "Referer": login_url,
        "User-Agent": ""."Accept-Encoding": "gzip, deflate"."Accept-Language": "zh-CN,zh; Q = 0.9, en - US; Q = 0.8, en. Q = 0.7, useful - TW; Q = 0.6, ru; Q = 0.5"."Connection": "keep-alive"
    }
Copy the code

Step 3: Request

With values and headers, we just need to call the session to do the request.

    session = requests.session()
    login_response = session.post(login_url, data=login_values, headers=login_header)
Copy the code

The session.post() method returns an HTTP response. If you want to see the result during development, you can see it in Debug mode.

Here’s a little more on why we use requests session instead of request. Get. The root cause is that the login status needs to be maintained for a long time. If we want to reply to a post, we need to keep an account logged in, and session can meet our needs. As long as the login is successful, the session saves state, mainly cookies. For the next series of requests, just call session.get() or session.post(). We get the cookie, request the server, the server will determine that we are logged in status. Why is that? That’s what cookies are for.

With the login complete, we now have to find the board and pick a reply to the post.

BeautifulSoup4 is BeautifulSoup4 and BeautifulSoup4 is BeautifulSoup4 and BeautifulSoup4 and BeautifulSoup4 are BeautifulSoup4 and BeautifulSoup4. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * I won’t say more here to save time.

Return card

So we’re going to jump right in here and we’re going to do post. Similarly, we need Charles to fetch the parameters of the POST request. Eventually we had to piece together our own value and header. The process is similar to logging in. After fetching and analyzing, we cobbled together a header that would look something like this:

    comment_header = {
        "Referer": post_url,
        "User-Agent": "Mozilla / 5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"."Accept-Encoding": "gzip, deflate"."Accept-Language": "zh-CN,zh; Q = 0.9, en - US; Q = 0.8, en. Q = 0.7, useful - TW; Q = 0.6, ru; Q = 0.5"."Connection": "keep-alive"."Content-Length": "252"."Cache-Control": "max-age=0"
    }
Copy the code

As for the composition of form data, by analyzing HTML data, we found that the form data in post request is pieced together by making all the “name” and “value” of the INPUT under the HTML form tag into key-value pairs. BeautifulSoup captures data to form key-value pairs to construct form data.

So finally we get the previous session object and we send a POST request.

comment_response = session.post(post_base_url, data=post_data, headers=comment_header)
Copy the code

DONE. The above is simulated login and automatic post code.

The timer

Due to the special nature of the forum, the reply interval must be 1024 seconds, so we also need to make a module that executes tasks regularly to execute our reply code. Here I use Python’s os.system() method to execute the module. For 1024 seconds, just write a while loop and count the time to do the corresponding operation. Python’s Datatime library still provides a wealth of operations and is very convenient.

def run_Task(a):
    os.system("python login.py")

def clock_timer(schedule_time):
    count = 0
    flag = 0
    while True:
        if count == 11:
            print("------- Count time is 11. FINISHED.")
            break
        cur_time = datetime.datetime.now()
        if cur_time == schedule_time:
            print("------- Run time: " + str(cur_time) + "\n------- CountTime: " + str(count))
            count = count + 1
            run_Task()
            flag = 1
        else:
            if flag == 1:
                flag = 0
                schedule_time = schedule_time + datetime.timedelta(seconds=1050)
                print("------- Next schedule time: " + str(schedule_time))

if __name__ == '__main__':
    cur_time = datetime.datetime.now()
    print("------- Start time: " + str(cur_time))
    schedule_time = cur_time + datetime.timedelta(seconds=2)
    print("------- Schedule time: " + str(cur_time))
    clock_timer(schedule_time)
Copy the code

Every day only need to perform the scheduled task of the code can be very simple and convenient. 10 times a day, easy to do. I believe that in a few days, you can upgrade to a “chivalrous”, ha ha ha ah.

This is the code, pay attention to the public number “pique pa shovel excrement officer”, reply “reply” can get the source code. There are surprises in store for you

Finally, the most critical thing in the article is to talk about the idea, the website forum, as long as you master the idea, you can deal with all. We have any questions, you can leave a message to the public number, or the article message, I will answer one by one. We make progress together.

Recommended reading:

This is how you get to the top of your game and get to the top of your game. This is how you get to the top of your game and get to the top of your game. This is how you get to the top of your game