Build a Python Weibo Monitor: Simulate Login and Fetch Posts
This tutorial walks through building a Weibo monitoring tool in Python by simulating mobile login, extracting API endpoints, fetching user posts via JSON, handling containers, and implementing a session‑based class that polls for new tweets and sends email alerts.
0x00 Preface
After weeks of struggling with character‑encoding issues in a Weibo monitoring project, a new mobile API (https://m.weibo.cn/) was discovered that meets the requirements.
0x01 Analysis
Direct access to the API triggers a 302 redirect to a login page, so a simulated login is required. Packet analysis shows that a correct username and password return HTTP 200 with a JSON retcode of 20000000, and no captcha is needed.
After logging in, the user’s homepage (e.g., https://m.weibo.cn/u/3023940914) loads two XHR requests:
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
The first URL can be accessed by adding type and value parameters, while the containerid for the second request is obtained from the first response.
The second request returns a JSON where each post appears in the cards list; each card’s mblog array contains the post text, images, source, and timestamp. The card_type field indicates the post type (text, image, video, etc.), with text and image posts sharing card_type 9.
Initially the monitor focuses on text and image posts.
0x02 Development
Login simulation is performed with requests.session() . The following code snippets illustrate the process:
After a successful login, the first API URL is constructed with the user ID, and the containerid is retrieved to request the posts JSON.
All fetched post IDs are stored; subsequent polls compare new IDs to detect newly published posts.
The entire logic is encapsulated in a Python class (full source shown in the images below) and hosted on GitHub: https://github.com/naiquann/WBMonitor .
<del>其实就是懒</del>0x03 Test
Running the script requires filling in login credentials and the target user ID. A heartbeat request is sent every three seconds to check for new posts; the interval can be adjusted for production use.
When a new post appears, the monitor triggers an email notification.
All set – the Weibo monitor is now operational.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
