# How to Use Phone Number Login in MediaCrawler: A Complete Guide

> Learn how to use phone number login in MediaCrawler with our complete guide. Configure CLI and environment variables for seamless access. NanmiCoder/MediaCrawler

- Repository: [程序员阿江-Relakkes/MediaCrawler](https://github.com/NanmiCoder/MediaCrawler)
- Tags: how-to-guide
- Published: 2026-06-27

---

**Phone number login in MediaCrawler requires setting `--lt phone` via CLI and providing your number through the `config.LOGIN_PHONE` configuration variable, which triggers the platform-specific `login_by_mobile()` method.**

MediaCrawler supports multiple authentication methods across Chinese social media platforms, with phone-based login offering a direct alternative to QR codes and cookies. This guide explains how to configure and execute phone number authentication using the actual implementation found in the `NanmiCoder/MediaCrawler` repository.

## How Phone Number Login Works in MediaCrawler

The phone login system operates through three architectural layers that handle configuration, delegation, and execution.

### Architecture Overview

1. **CLI and Configuration Layer** ([`cmd_arg/arg.py`](https://github.com/NanmiCoder/MediaCrawler/blob/main/cmd_arg/arg.py)): Parses the `--lt` (login type) flag and stores the value in `config.LOGIN_TYPE`. The phone number is retrieved from `config.LOGIN_PHONE`, which accepts values from environment variables or direct configuration.

2. **Platform Core Layer** (`media_platform/<platform>/core.py`): Instantiates the appropriate login class (e.g., `ZhiHuLogin`, `XhsLogin`) and forwards the phone number to the constructor via the `login_phone` parameter.

3. **Login Implementation Layer** (`media_platform/<platform>/login.py`): Executes the `login_by_mobile()` coroutine when `LOGIN_TYPE` equals `"phone"`, handling the actual authentication flow with the target platform's API.

## Command Line Usage

The most common approach uses environment variables to inject the phone number before invoking the main entry point.

```bash

# Set the phone number environment variable

export LOGIN_PHONE=13800138000

# Execute with phone login type

python -m MediaCrawler.main \
    --platform zhihu \
    --lt phone \
    --keywords "AI,ML"

```

This pattern works across all supported platforms:

| Platform | CLI Flag | Command Example |
|----------|----------|----------------|
| XiaoHongShu | `xhs` | `python -m MediaCrawler.main --platform xhs --lt phone` |
| Douyin | `dy` | `python -m MediaCrawler.main --platform dy --lt phone` |
| Kuaishou | `ks` | `python -m MediaCrawler.main --platform ks --lt phone` |
| Bilibili | `bili` | `python -m MediaCrawler.main --platform bili --lt phone` |
| Weibo | `wb` | `python -m MediaCrawler.main --platform wb --lt phone` |
| Tieba | `tieba` | `python -m MediaCrawler.main --platform tieba --lt phone` |

## Programmatic Usage

When embedding MediaCrawler in Python applications, you can configure phone login programmatically before initializing the platform core.

### Using the Platform Core

```python
import os
from media_platform.zhihu.core import ZhiHuCore
from config import base_config as cfg

# Configure login credentials

cfg.LOGIN_TYPE = "phone"
cfg.LOGIN_PHONE = "13800138000"

# Initialize browser context (Playwright)

browser_context = ...  # BrowserContext instance

page = ...             # Page instance

# Create platform instance with phone number

zhihu = ZhiHuCore(
    login_type=cfg.LOGIN_TYPE,
    browser_context=browser_context,
    context_page=page,
    login_phone=cfg.LOGIN_PHONE,
)

# Trigger authentication

await zhihu.login()

```

### Direct Login Class Invocation

For advanced use cases, instantiate the login class directly:

```python
from media_platform.weibo.login import WeiboLogin
from playwright.async_api import async_playwright

async def phone_login_demo():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        context = await browser.new_context()
        page = await context.new_page()
        
        login = WeiboLogin(
            login_type="phone",
            browser_context=context,
            context_page=page,
            login_phone="13800138000",
        )
        await login.begin()  # Executes login_by_mobile()

```

## Configuration Options

You can provide the phone number through three methods:

- **Config file**: Edit [`config/base_config.py`](https://github.com/NanmiCoder/MediaCrawler/blob/main/config/base_config.py) and set `LOGIN_PHONE = "1234567890"`
- **Environment variable**: Export `LOGIN_PHONE=1234567890` before running
- **Programmatic**: Set `cfg.LOGIN_PHONE` directly in your Python code as shown above

The [`cmd_arg/arg.py`](https://github.com/NanmiCoder/MediaCrawler/blob/main/cmd_arg/arg.py) file defines the `LoginTypeEnum` which validates that `"phone"` is an accepted value for the `--lt` flag.

## Platform Support and Limitations

Phone number login in MediaCrawler depends on platform-specific implementations in files like [`media_platform/zhihu/login.py`](https://github.com/NanmiCoder/MediaCrawler/blob/main/media_platform/zhihu/login.py). If a platform's `login_by_mobile()` method contains a placeholder marked "todo implement login by mobile", attempting phone authentication will raise a `ValueError` indicating the feature is not yet supported for that platform.

## Summary

- Set `--lt phone` via command line to activate phone number authentication mode
- Provide your phone number through `config.LOGIN_PHONE` using environment variables, config files, or programmatic assignment
- The platform core in `media_platform/<platform>/core.py` delegates to `login_by_mobile()` in the corresponding login class
- Not all platforms have complete implementations; check for "todo" placeholders in the specific login file before attempting

## Frequently Asked Questions

### How do I set the phone number for login in MediaCrawler?

You can set the phone number by exporting the `LOGIN_PHONE` environment variable before execution, modifying `LOGIN_PHONE` in [`config/base_config.py`](https://github.com/NanmiCoder/MediaCrawler/blob/main/config/base_config.py), or setting `cfg.LOGIN_PHONE` programmatically when using the library. The value is passed to the login class constructor as the `login_phone` parameter.

### Which platforms support phone number login in MediaCrawler?

MediaCrawler includes phone login infrastructure for Zhihu, XiaoHongShu (XHS), Douyin, Kuaishou, Bilibili, Weibo, and Tieba. However, actual implementation completeness varies by platform. Check the specific [`login.py`](https://github.com/NanmiCoder/MediaCrawler/blob/main/login.py) file in `media_platform/<platform>/` to verify if `login_by_mobile()` is fully implemented or marked as a todo.

### What happens if phone login is not implemented for a platform?

If the `login_by_mobile()` method is not implemented for your chosen platform, MediaCrawler will raise a `ValueError` stating that the phone login type is not supported. In this case, you should fall back to QR code login (`--lt qrcode`) or cookie-based authentication (`--lt cookie`) for that specific platform.

### Can I use phone login without the command line?

Yes, you can use phone login programmatically by importing `base_config` from `config`, setting `LOGIN_TYPE = "phone"` and `LOGIN_PHONE` to your number, then instantiating the appropriate platform core class (e.g., `ZhiHuCore`) with these configuration values and calling `await core.login()`.