search_topics
Find trending discussions on Weibo by entering keywords to discover related topics and conversations.
Instructions
Search for topics on Weibo based on a keyword.
Returns:
list[dict]: List of dictionaries containing search results
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| keyword | Yes | Search term to find content | |
| limit | No | Maximum number of results to return, defaults to 15 | |
| page | No | Page number for pagination, defaults to 1 |
Implementation Reference
- src/mcp_server_weibo/server.py:98-111 (handler)MCP tool handler function for 'search_topics', decorated with @mcp.tool(). It defines the input schema via Annotated Fields and delegates execution to WeiboCrawler.search_topics.@mcp.tool() async def search_topics( ctx: Context, keyword: Annotated[str, Field(description="Search term to find content")], limit: Annotated[int, Field(description="Maximum number of results to return, defaults to 15", default=15)] = 15, page: Annotated[int, Field(description="Page number for pagination, defaults to 1", default=1)] = 1 ) -> list[dict]: """ Search for topics on Weibo based on a keyword. Returns: list[dict]: List of dictionaries containing search results """ return await crawler.search_topics(keyword, limit, page)
- Core implementation of the search_topics method in WeiboCrawler class, performing HTTP request to Weibo search API for topics (containerid type=38) and processing results.async def search_topics(self, keyword: str, limit: int = 15, page: int = 1) -> list[dict]: """ Search Weibo topics by keyword. Args: keyword (str): The search keyword limit (int): Maximum number of topic results to return, defaults to 15 page (int, optional): The starting page number, defaults to 1 Returns: list[dict: List of dict containing topic search results """ async with httpx.AsyncClient() as client: try: params = { 'containerid': f'100103type=38&q={keyword}', 'page_type': 'searchall', 'page': page, } encoded_params = urlencode(params) response = await client.get(f'{SEARCH_URL}?{encoded_params}', headers=DEFAULT_HEADERS) result = response.json() cards = result["data"]["cards"] if len(cards) < 1: return [] else: cardGroup = cards[0]['card_group'] return [self._to_topic_item(item) for item in cardGroup][:limit] except httpx.HTTPError: self.logger.error( f"Unable to search users for keyword '{keyword}'", exc_info=True) return []
- Helper method to format individual topic search results into a standardized dictionary.def _to_topic_item(self, item: dict) -> dict: """ Convert raw topic data to a formatted dictionary. Args: item (dict): Raw topic data from Weibo API Returns: dict: Formatted topic information """ return { 'title': item['title_sub'], 'desc1': item.get('desc1', ''), 'desc2': item.get('desc2', ''), 'url': item.get('scheme', '') }
- src/mcp_server_weibo/server.py:98-111 (registration)The @mcp.tool() decorator registers this function as an MCP tool named 'search_topics'.@mcp.tool() async def search_topics( ctx: Context, keyword: Annotated[str, Field(description="Search term to find content")], limit: Annotated[int, Field(description="Maximum number of results to return, defaults to 15", default=15)] = 15, page: Annotated[int, Field(description="Page number for pagination, defaults to 1", default=1)] = 1 ) -> list[dict]: """ Search for topics on Weibo based on a keyword. Returns: list[dict]: List of dictionaries containing search results """ return await crawler.search_topics(keyword, limit, page)