Switching MediaCrawler Login from QR Code to Phone Number Authentication: A Complete Guide
Changing MediaCrawler's authentication method requires only updating the LOGIN_TYPE configuration variable to "phone" or passing --lt phone via command line.
MediaCrawler is an open-source scraping framework that unifies data collection across Chinese social platforms. Switching MediaCrawler login from QR code to phone number authentication is controlled centrally through a single configuration flag, making the transition seamless across all supported platforms without modifying platform-specific logic.
Understanding the Centralized Login Architecture
The MediaCrawler project implements a unified login gateway that supports three authentication methods: QR code, phone number, and cookie. All methods are gated by the LOGIN_TYPE constant defined in config/base_config.py.
# config/base_config.py
LOGIN_TYPE = "qrcode" # qrcode, phone, cookie
Each platform module—including Zhihu, XiaoHongShu, Weibo, Douyin, Bilibili, Kuaishou, and Tieba—contains a dedicated login class that reads this flag and routes to the appropriate authentication flow. This design means changing authentication modes requires zero code changes within individual platform implementations.
Method 1: Configuration File Approach
The simplest way to switch authentication methods is editing the base configuration file. Open config/base_config.py and change the LOGIN_TYPE value:
LOGIN_TYPE = "phone" # Switches from qrcode to phone authentication
This single-line change instructs every platform module to use the login_by_phone() method instead of login_by_qrcode(). The configuration serves as the single source of truth across the entire application.
Method 2: Command Line Interface Override
For temporary switches or automated scripts, use the --lt (login type) argument exposed in cmd_arg/arg.py. The CLI parser maps this argument directly to the LOGIN_TYPE constant:
# Run with QR code (default)
python main.py --platform weibo
# Switch to phone authentication
python main.py --platform weibo --lt phone
# Alternative platforms
python main.py --platform xhs --lt phone
python main.py --platform douyin --lt phone
The argument definition in cmd_arg/arg.py accepts three valid values: qrcode, phone, or cookie.
Platform-Specific Login Routing
Under the hood, each platform's login class checks config.LOGIN_TYPE and branches accordingly. In media_platform/zhihu/login.py, the logic appears as:
# media_platform/zhihu/login.py
if config.LOGIN_TYPE == "qrcode":
await self.login_by_qrcode()
elif config.LOGIN_TYPE == "phone":
await self.login_by_phone()
else:
raise ValueError("[ZhiHu.begin]Invalid Login Type Currently only supported qrcode or phone or cookies …")
This pattern repeats across all platform modules in media_platform/, ensuring consistent behavior whether scraping XiaoHongShu, Weibo, or Kuaishou.
Phone Login Implementation Details
When LOGIN_TYPE is set to "phone", the crawler executes login_by_phone(), which automates the browser-based phone authentication flow. The implementation in media_platform/douyin/login.py demonstrates the core mechanics:
# media_platform/douyin/login.py
input_ele = await login_container_ele.query_selector("label.phone > input")
await input_ele.fill(self.login_phone) # Fills phone number
sms_code_key = f"dy_{self.login_phone}" # Cache key for Redis
The workflow follows three stages:
- Fill: Enters the phone number into the
label.phone > inputselector - Request: Triggers the SMS code request via
request_sms_code() - Submit: Retrieves the verification code from cache (typically Redis) and submits it
Supplying the Phone Number
The target phone number is provided when constructing the login class instance. All platform login classes accept a login_phone parameter:
from media_platform.weibo.login import WeiboLogin
# Initialize with phone number
login = WeiboLogin(login_phone="13800138000")
await login.begin()
This constructor pattern is consistent across WeiboLogin, XHSLogin, DouyinLogin, and other platform-specific implementations.
Supported Platforms
Phone authentication is fully implemented across the following platform modules:
media_platform/zhihu/login.pymedia_platform/xhs/login.py(XiaoHongShu)media_platform/weibo/login.pymedia_platform/douyin/login.pymedia_platform/bilibili/login.pymedia_platform/kuaishou/login.pymedia_platform/tieba/login.py
Each module contains a complete login_by_phone() implementation following the standardized input-filling and SMS verification pattern.
Summary
- Single configuration point: Change
LOGIN_TYPEinconfig/base_config.pyor use--lt phoneCLI argument to switch authentication methods - Zero platform modifications: Individual platform modules automatically route to
login_by_phone()based on the central configuration flag - Unified interface: All platforms accept
login_phoneparameter and implement consistent SMS verification workflows - Redis integration: SMS codes are cached using platform-specific keys (e.g.,
dy_{phone}for Douyin) to support automated retrieval
Frequently Asked Questions
What values are valid for the LOGIN_TYPE configuration?
The LOGIN_TYPE variable in config/base_config.py accepts three string values: "qrcode" for QR code scanning, "phone" for phone number with SMS verification, and "cookie" for existing session authentication. Any other value triggers a ValueError in the platform login classes.
Do I need to modify platform-specific code to switch from QR code to phone login?
No. Switching MediaCrawler login from QR code to phone number authentication requires no changes to files in media_platform/. The routing logic in each platform's login class (e.g., media_platform/zhihu/login.py) already contains conditional checks for config.LOGIN_TYPE. Simply change the configuration value or use the --lt CLI argument.
How does MediaCrawler handle SMS verification codes during phone authentication?
The phone login implementation automatically fills the phone number into the web form, triggers the SMS request, and retrieves the verification code from a Redis cache. The cache key typically follows the pattern {platform_prefix}_{phone_number} (e.g., dy_13800138000 for Douyin), allowing external processes to inject the received code into the scraping workflow.
Where can I find additional documentation for phone-based login?
The repository includes a dedicated user guide at docs/手机号登录说明.md (Phone Login Instructions) that provides detailed setup instructions, SMS integration guidelines, and troubleshooting steps for running the crawler with phone authentication enabled.
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:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →