Backend Development 10 min read

Automating Weibo Login, Posting, and Liking with Python Requests

This tutorial demonstrates how to use Python's requests library to programmatically log into Weibo, publish a status update, and automatically like posts by handling cookies, sessions, and the required st token, complete with full code examples.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Automating Weibo Login, Posting, and Liking with Python Requests

This article explains how to automate Weibo operations—login, posting a status, and liking posts—using Python's requests library. It first shows how to capture the login request in the browser, replicate the necessary headers (user‑agent, referer, cookie) and POST data, and then perform the login via code.

Next, it describes how to obtain the dynamic st token required for posting by requesting the /api/config endpoint, extracting the token from the JSON response, and sending a POST request to https://m.weibo.cn/api/statuses/update with the status content and the token.

Finally, the guide provides a class‑based implementation that logs in, retrieves the st token, fetches a list of the user's Weibo posts via https://m.weibo.cn/api/container/getIndex , and iterates over the posts (card_type 9) to send a like request to https://m.weibo.cn/api/attitudes/create using the post ID and the token.

Throughout the article, the author notes that cookies are not permanent and must be refreshed when login fails, and that the referer , origin , and host headers are often required to bypass anti‑scraping measures.

Full code snippets are provided for each step, including a simple script, a more structured class version, and a batch‑like function that likes all retrieved posts.

<code>import requests
headers = {
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
    'referer': 'https://passport.weibo.cn/signin/login?...',
    'cookie': 'your_cookie'
}
login_data = {
    'savestate': '1',
    'r': 'https://m.weibo.cn/?jumpfrom=weibocom',
    'ec': '0',
    'pagerefer': 'https://m.weibo.cn/login?...',
    'entry': 'mweibo',
    ...
}
session = requests.Session()
session.headers.update(headers)
login_req = session.post('https://passport.weibo.cn/sso/login', data=login_data)
print(login_req.status_code)  # 200 means success

config_req = session.get('https://m.weibo.cn/api/config')
st = config_req.json()['data']['st']
compose_data = {'content': input('Enter content:'), 'st': st}
compose_req = session.post('https://m.weibo.cn/api/statuses/update', data=compose_data)
print(compose_req.json())
</code>
<code>class WeiboSpider:
    def __init__(self):
        self.session = requests.Session()
        self.headers = {
            'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
            'referer': 'https://passport.weibo.cn/signin/login?...',
            'cookie': 'your_cookie'
        }
        self.session.headers.update(self.headers)
    def login(self):
        ...  # same login_data as above
    def get_st(self):
        config = self.session.get('https://m.weibo.cn/api/config').json()
        return config['data']['st']
    def compose(self, content):
        data = {'content': content, 'st': self.get_st()}
        resp = self.session.post('https://m.weibo.cn/api/statuses/update', data=data)
        print(resp.json())
    def get_weibo_list(self):
        params = {'type': 'uid', 'value': '2139359753', 'containerid': '1076032139359753'}
        data = self.session.get('https://m.weibo.cn/api/container/getIndex', params=params).json()
        return data['data']['cards']
    def vote_up(self, id):
        data = {'id': id, 'attitude': 'heart', 'st': self.get_st()}
        resp = self.session.post('https://m.weibo.cn/api/attitudes/create', data=data)
        print(resp.json()['msg'])
    def vote_up_all(self):
        self.login()
        for card in self.get_weibo_list():
            if card['card_type'] == 9:
                self.vote_up(card['mblog']['id'])

weibo = WeiboSpider()
weibo.vote_up_all()
</code>
APIrequestsWeb AutomationWeiboscraping
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login 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.