How to Use MediaCrawler for Tieba Data: A Complete Guide to Baidu Tieba Scraping

MediaCrawler harvests Baidu Tieba data through Playwright-driven browser automation, supporting keyword search, specific thread extraction, and creator profile scraping via configurable CLI or Python API.

MediaCrawler is an open-source multi-platform crawler that treats Baidu Tieba as a first-class data source. To use MediaCrawler for Tieba data, you configure the tieba platform flag and select from three crawling modes—search, detail, or creator—each implemented in the media_platform/tieba/core.py module. The architecture uses Playwright to manage browser contexts and anti-detection scripts, ensuring reliable access to Tieba's PC JSON APIs.

Understanding the Tieba Crawling Pipeline

The Tieba implementation follows a strict pipeline defined in media_platform/tieba/core.py, where the TieBaCrawler class orchestrates each phase of data collection.

Initialization and Browser Setup

When you instantiate TieBaCrawler, the __init__ method reads global settings from config.py and launches a Playwright browser context. The system supports both standard browser mode and Chrome DevTools Protocol (CDP) mode, automatically injecting anti-detection scripts to avoid fingerprinting. If ENABLE_IP_PROXY is set, the crawler refreshes proxy IPs during this phase.

Authentication Flow

Before extracting data, the crawler verifies login state via BaiduTieBaClient.pong. If valid cookies are missing, the BaiduTieBaLogin class triggers a QR-code or phone-based login flow. When config.SAVE_LOGIN_STATE is true, the browser creates a persistent profile in browser_data/, allowing you to authenticate once and reuse the session across runs.

Crawler Type Selection

The config.CRAWLER_TYPE variable determines the operational mode:

  • search: Harvests threads matching KEYWORDS via Tieba's search endpoint.
  • detail: Fetches specific threads identified in TIEBA_SPECIFIED_ID_LIST.
  • creator: Scrapes all posts from user profiles listed in TIEBA_CREATOR_URL_LIST.

The TieBaCrawler.start method routes execution based on this configuration.

API Client and Data Extraction

The create_tieba_client method builds a BaiduTieBaClient instance (defined in media_platform/tieba/client.py) equipped with the current browser user-agent and cookies. This client communicates with Tieba's PC JSON APIs, signing requests with PC_SIGN_SECRET. Raw responses pass through TieBaExtractor (in media_platform/tieba/help.py), which converts JSON/HTML into typed Pydantic models: TiebaNote, TiebaComment, and TiebaCreator.

Storage and Shutdown

Extracted objects flow to the storage abstraction layer in store/tieba/_store_impl.py. Depending on SAVE_DATA_OPTION, the system routes data to CSV files (via AsyncFileWriter), JSON lines, or MongoDB (via MongoDBStoreBase). Finally, TieBaCrawler.close terminates the browser context and cleans up resources.

Configuration Options for Tieba Scraping

All configuration flags are parsed in cmd_arg/arg.py (specifically the parse_cmd function) and written to the global config module before crawling begins.

Core Crawler Settings

  • PLATFORM: Must be set to tieba to activate the Tieba pipeline.
  • CRAWLER_TYPE: Controls the scraping mode (search, detail, or creator).
  • KEYWORDS: Comma-separated search terms for the search mode.
  • TIEBA_SPECIFIED_ID_LIST: Thread IDs or full URLs (e.g., https://tieba.baidu.com/p/123456789) for the detail mode.
  • TIEBA_CREATOR_URL_LIST: Creator homepage URLs for the creator mode.
  • ENABLE_GET_COMMENTS and ENABLE_GET_SUB_COMMENTS: Boolean flags to fetch first-level and nested comments.

Proxy and Browser Configuration

  • HEADLESS / CDP_HEADLESS: Run browsers without GUI when set to true.
  • SAVE_DATA_OPTION: Choose between csv, jsonl, or mongodb output formats.
  • ENABLE_IP_PROXY, STATIC_PROXY_URL: Enable and configure proxy rotation to avoid IP bans.

Data Extraction and Modeling

The extraction logic resides in media_platform/tieba/help.py within the TieBaExtractor class.

The TieBaExtractor Class

This utility class provides static methods for parsing Tieba's API responses:

  • extract_search_note_list_from_api: Parses PC search JSON results.
  • extract_note_detail_from_api: Extracts thread content and metadata.
  • extract_tieba_note_parent_comments_from_api: Retrieves top-level comments.
  • extract_tieba_note_sub_comments: Retrieves reply-to-reply comments.
  • extract_creator_info_from_api and extract_creator_thread_id_list_from_api: Handle creator profile data.

Pydantic Data Models

All extracted data validates against models defined in model/m_baidu_tieba.py:

  • TiebaNote: Contains title, publish_time, user_nickname, tieba_name, and content fields.
  • TiebaComment: Structures comment hierarchies with user info and timestamps.
  • TiebaCreator: Stores profile metadata and post associations.

Storage Backends

The generic store interface in store/tieba/__init__.py dispatches to concrete implementations in store/tieba/_store_impl.py. The crawler calls specific functions like update_tieba_note for threads, batch_update_tieba_note_comments for comment batches, and save_creator for user profiles. This design decouples extraction from persistence, allowing you to route the same data to multiple backends simultaneously.

Implementation Examples

Command-Line Usage

Install dependencies and initialize Playwright:

pip install -r requirements.txt
playwright install chromium

Execute a keyword search:

python -m MediaCrawler.main --platform tieba --type search \
       --keywords python \
       --headless true \
       --save_data_option csv

Fetch specific threads by ID:

python -m MediaCrawler.main --platform tieba --type detail \
       --specified_id "https://tieba.baidu.com/p/123456789,987654321" \
       --headless false \
       --save_data_option jsonl

Scrape a creator's profile:

python -m MediaCrawler.main --platform tieba --type creator \
       --creator_id "https://tieba.baidu.com/home/main?id=tb.1.example" \
       --headless true \
       --save_data_option mongodb

Programmatic Python API

Embed the crawler in your own async workflows:

import asyncio
from media_platform.tieba.core import TieBaCrawler
import config

async def main():
    config.PLATFORM = "tieba"
    config.CRAWLER_TYPE = "detail"
    config.TIEBA_SPECIFIED_ID_LIST = ["123456789", "987654321"]
    config.HEADLESS = True
    config.SAVE_DATA_OPTION = "jsonl"

    crawler = TieBaCrawler()
    await crawler.start()
    await crawler.close()

asyncio.run(main())

Direct Data Access

Bypass the storage layer to work with raw Pydantic objects:

from media_platform.tieba.client import BaiduTieBaClient
from playwright.async_api import async_playwright
import asyncio

async def fetch_one_thread(thread_id: str):
    async with async_playwright() as pw:
        browser = await pw.chromium.launch(headless=True)
        context = await browser.new_context()
        page = await context.new_page()
        
        client = BaiduTieBaClient(playwright_page=page)
        note = await client.get_note_by_id(thread_id)
        print(note.json(indent=2))
        
        await browser.close()

asyncio.run(fetch_one_thread("123456789"))

Summary

  • MediaCrawler for Tieba data runs through the TieBaCrawler class in media_platform/tieba/core.py, which manages Playwright browser contexts and anti-detection measures.
  • The system supports three operational modes—search, detail, and creator—controlled via config.CRAWLER_TYPE.
  • Configuration is handled through CLI arguments (parsed in cmd_arg/arg.py) or direct config module manipulation, with key variables including TIEBA_SPECIFIED_ID_LIST and ENABLE_GET_COMMENTS.
  • Data extraction relies on TieBaExtractor methods in media_platform/tieba/help.py to parse PC JSON APIs into strict Pydantic models.
  • Results persist through pluggable backends in store/tieba/_store_impl.py, supporting CSV, JSON lines, and MongoDB outputs.

Frequently Asked Questions

What is the difference between search, detail, and creator modes in MediaCrawler for Tieba?

Search mode scans Tieba's index for threads matching your KEYWORDS, harvesting thread metadata and content. Detail mode fetches complete data for specific thread IDs provided in TIEBA_SPECIFIED_ID_LIST, including full comment trees if enabled. Creator mode scrapes all posts from user profiles listed in TIEBA_CREATOR_URL_LIST, useful for influencer analysis or archival.

How does MediaCrawler avoid detection when scraping Tieba?

The crawler uses Playwright to execute JavaScript in real Chromium instances, applying anti-detection scripts during initialization in TieBaCrawler.__init__. It mimics legitimate browser headers, manages cookies via BaiduTieBaClient, and supports IP proxy rotation through ENABLE_IP_PROXY to distribute request load across multiple addresses.

Can I use MediaCrawler for Tieba data without logging in?

While some public threads are accessible anonymously, BaiduTieBaClient.pong checks for valid authentication cookies. If none exist, the system triggers BaiduTieBaLogin to generate a QR code. You can complete login once and set SAVE_LOGIN_STATE to true, which stores credentials in browser_data/ for subsequent headless runs.

Where are the scraped Tieba results stored?

Storage location depends on SAVE_DATA_OPTION. When set to csv, data writes to the designated output directory via AsyncFileWriter. For jsonl, files append as line-delimited JSON. The mongodb option routes documents through MongoDBStoreBase to your configured database. All storage logic resides in store/tieba/_store_impl.py.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →