How to Build a Weibo Monitoring Tool with Python Requests

This article walks through building a Weibo monitoring tool using Python's requests library, covering API analysis, login simulation, endpoint handling, data extraction, and a simple polling mechanism that detects new posts and sends email alerts.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Build a Weibo Monitoring Tool with Python Requests

0x00 Preface

In recent weeks I attempted to write a Weibo monitoring system, encountering several setbacks due to character‑encoding issues that halted development. Yesterday I discovered a mobile Weibo interface (https://m.weibo.cn/) that meets my requirements, so I resumed the project.

0x01 Analysis

The interface redirects to a login page with a 302 response, so a simulated login is required before accessing Weibo data.

By capturing network traffic I found that a correct username and password return HTTP 200 and a JSON field retcode with value 20000000. No captcha is required, making login simulation straightforward.

After logging in, visiting a user’s homepage (e.g., https://m.weibo.cn/u/3023940914) reveals two XHR requests that load user information and Weibo posts.

The relevant API endpoints are:

https://m.weibo.cn/api/container/getIndex?type=uid&value=3023940914&containerid=1005053023940914

https://m.weibo.cn/api/container/getIndex?type=uid&value=3023940914&containerid=1076033023940914

Adding the type and value parameters to the first URL works without the containerid parameter. The containerid for the second URL is obtained from the first call.

Using the retrieved containerid , the second endpoint returns the Weibo posts of the specified UID.

The JSON response contains a cards list; each card’s mblog array holds the post text, images, source, and timestamp. The card_type field indicates the post type (text, image, video, etc.). Tests show that both text and image posts share the same card_type value of 9.

At this stage the tool focuses on monitoring text and image posts.

<del>其实就是懒</del>

0x02 Development

The first step is to simulate login using a persistent requests.session() . The following code snippets illustrate the process.

After a successful login, the user ID is concatenated with the first API URL to fetch basic information, then the containerid is added to retrieve the full list of posts.

All retrieved post IDs are stored; subsequent runs compare new IDs against this list to detect newly published Weibo entries.

The complete functionality is encapsulated in a Python class (full source shown in the images below).

A simple email notification is added to alert when a new Weibo post is detected; the full code is available on GitHub at https://github.com/naiquann/WBMonitor.

0x03 Testing

Running the script prompts for login credentials and the target user ID. After configuration, a heartbeat request is sent every three seconds to check for new posts.

When a new post appears, the script prints the post details and sends an email alert.

All tests confirm that the monitoring tool works as expected.

Done! The Weibo monitor is ready to track new posts.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonAutomationAPI monitoringWeibo
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.