NanmiCoder/MediaCrawler · error

playwright_page is required for browser-based search

Error message

playwright_page is required for browser-based search

What it means

Raised by BaiduTieBaClient.get_notes_by_keyword (media_platform/tieba/client.py:396) when self.playwright_page is None. Tieba search is implemented exclusively through a live Playwright page context (to bypass API signature detection), so without a browser page the method cannot run and refuses with an explicit guard rather than an AttributeError.

Source

Thrown at media_platform/tieba/client.py:396

        page: int = 1,
        page_size: int = 10,
        sort: SearchSortType = SearchSortType.TIME_DESC,
        note_type: SearchNoteType = SearchNoteType.FIXED_THREAD,
    ) -> List[TiebaNote]:
        """
        Search Tieba posts by keyword (uses Playwright to access page, avoiding API detection)
        Args:
            keyword: Keyword
            page: Page number
            page_size: Page size
            sort: Result sort method
            note_type: Post type (main thread | main thread + reply mixed mode)
        Returns:

        """
        if not self.playwright_page:
            utils.logger.error("[BaiduTieBaClient.get_notes_by_keyword] playwright_page is None, cannot use browser mode")
            raise Exception("playwright_page is required for browser-based search")

        params = {
            "rn": max(page_size, 20),
            "st": sort.value,
            "word": keyword,
            "needbrand": 1,
            "sug_type": 2,
            "pn": page,
            "come_from": "search",
            "subapp_type": "pc",
            "_client_type": "20",
        }
        utils.logger.info(
            f"[BaiduTieBaClient.get_notes_by_keyword] Accessing search API: "
            f"{self._host}/mo/q/search/multsearch?{urlencode(params)}"
        )

        try:

View on GitHub (pinned to d6f7c5bb90)

Solutions

  1. Launch a Playwright browser, create a context/page, and pass/set playwright_page on the client before searching
  2. Ensure the crawler's login flow (which creates the page) has completed before keyword search begins
  3. If you only need API-based data, use the API-backed methods (e.g. get_notes_by_tieba_name still requires a page — use the HTTP-frs endpoints) or a different platform client

Example fix

# before
client = BaiduTieBaClient()
notes = await client.get_notes_by_keyword('python')

# after
browser = await playwright.chromium.launch(headless=False)
context = await browser.new_context()
page = await context.new_page()
client = BaiduTieBaClient(playwright_page=page)
notes = await client.get_notes_by_keyword('python')
Defensive patterns

Strategy: type-guard

Validate before calling

if not getattr(client, "playwright_page", None):
    raise RuntimeError("Start browser and attach playwright_page before keyword search")

Type guard

def has_browser_page(client) -> bool:
    page = getattr(client, "playwright_page", None)
    return page is not None and not page.is_closed()

Prevention

When it happens

Trigger: Instantiating BaiduTieBaClient without a browser context and calling get_notes_by_keyword; or calling it before the crawler attaches the Playwright page (page not yet created/logged in); or after the browser was closed and the reference cleared.

Common situations: Using the client in a headless script/tests without starting Playwright; a code path that constructs the client for pure HTTP API calls and then reuses it for search; race where search starts before login finishes creating the page.

Related errors


AI-assisted analysis of NanmiCoder/MediaCrawler@d6f7c5bb90 (2026-08-15). Data as JSON: /api/errors/19df09a2a9ccd536. Report an issue: GitHub.