### Install Baidu Search via Pip Source: https://context7.com/amazingcoderpro/python-baidusearch/llms.txt The command to install the baidusearch package from the Python Package Index. ```bash pip install baidusearch ``` -------------------------------- ### Execute Baidu Search via Command Line Source: https://context7.com/amazingcoderpro/python-baidusearch/llms.txt Usage examples for the baidusearch CLI tool, supporting keyword queries, result limits, and debug flags. ```bash baidusearch "Python tutorial" baidusearch "web development" 15 baidusearch "data science" 10 1 ``` -------------------------------- ### Execute Baidu Search via CLI Source: https://github.com/amazingcoderpro/python-baidusearch/blob/master/README.md Run search queries directly from the command line using the installed baidusearch tool. ```shell baidusearch 长风破浪小武哥 ``` -------------------------------- ### Baidu Search API - Command Line Interface Source: https://context7.com/amazingcoderpro/python-baidusearch/llms.txt The package includes a CLI tool that can be invoked directly from the terminal after installation. It supports keyword arguments for result count and debug mode. ```APIDOC ## Command Line Interface ### Description Execute Baidu searches directly from the command line. ### Usage `baidusearch "" [num_results] [debug]` ### Parameters - **keyword** (string) - Required - The search term. - **num_results** (integer) - Optional - The maximum number of results to retrieve. - **debug** (integer) - Optional - Set to 1 to enable debug mode. ### Examples **Basic search:** ```bash baidusearch "Python tutorial" ``` **Specify number of results:** ```bash baidusearch "web development" 15 ``` **Enable debug mode:** ```bash baidusearch "data science" 10 1 ``` ### Example Output ``` ---start search: [Python tutorial], expected number of results:[10]. search results:(total[10]items.) 1. Python Tutorial - W3Schools Well organized and easy to understand Web building tutorials... http://www.baidu.com/link?url=... 2. Python教程 - 廖雪峰的官方网站 这是小白的Python新手教程,具有如下特点:中文,免费,零起点... http://www.baidu.com/link?url=... ``` ``` -------------------------------- ### Perform Baidu Searches in Python Source: https://context7.com/amazingcoderpro/python-baidusearch/llms.txt Demonstrates the core search functionality, including basic queries, custom result counts, debug mode, and Unicode support for non-English queries. ```python from baidusearch.baidusearch import search # Basic search results = search('Python programming') # Custom result count results = search('machine learning', num_results=20) # Debug mode results = search('artificial intelligence', num_results=15, debug=1) # Unicode/Chinese support results = search('人工智能', num_results=5) ``` -------------------------------- ### Implementing Robust Search with Rate Limiting Source: https://context7.com/amazingcoderpro/python-baidusearch/llms.txt A wrapper function for the search method that includes retry logic and mandatory delays to prevent IP blocking and handle rate limiting effectively. ```python from baidusearch.baidusearch import search import time def safe_search(keyword, num_results=10, retry_count=3): for attempt in range(retry_count): results = search(keyword, num_results=num_results) if results and len(results) > 0: return results wait_time = 15 * (attempt + 1) time.sleep(wait_time) return [] keywords = ['Python', 'JavaScript', 'Go language'] for keyword in keywords: results = safe_search(keyword, num_results=5) time.sleep(15) ``` -------------------------------- ### Accessing Baidu Search Result Fields Source: https://context7.com/amazingcoderpro/python-baidusearch/llms.txt Demonstrates the structure of a search result object returned by the library and how to iterate through a list of results to access specific fields like title, abstract, URL, and rank. ```python example_result = {'title': 'What is Cloud Computing? - Amazon Web Services', 'abstract': 'Cloud computing is the on-demand delivery of IT resources...', 'url': 'http://www.baidu.com/link?url=abc123...', 'rank': 1} for result in results: title = result['title'] abstract = result['abstract'] url = result['url'] rank = result['rank'] print(f"[{rank}] {title}") ``` -------------------------------- ### Perform Baidu Search Programmatically Source: https://github.com/amazingcoderpro/python-baidusearch/blob/master/README.md Use the search function to retrieve search results from Baidu. You can specify the number of results to return. ```python from baidusearch.baidusearch import search # Perform a basic search results = search('Full Stack Developer') # Perform a search with a custom result limit results = search('China', num_results=20) ``` -------------------------------- ### Baidu Search API - Main Search Function Source: https://context7.com/amazingcoderpro/python-baidusearch/llms.txt The primary function for performing Baidu searches. It accepts a keyword string, optional result count, and debug flag. Returns a list of dictionaries containing search results with title, abstract, URL, and rank fields. The function automatically handles pagination to retrieve the requested number of results. ```APIDOC ## GET /search ### Description Performs a search on Baidu using a given keyword and returns structured search results. ### Method GET ### Endpoint / ### Parameters #### Query Parameters - **keyword** (string) - Required - The search term. - **num_results** (integer) - Optional - The maximum number of results to retrieve. Defaults to 10. - **debug** (integer) - Optional - Set to 1 to enable debug mode for verbose output. Defaults to 0. ### Request Example ```python from baidusearch.baidusearch import search # Basic search - returns up to 10 results by default results = search('Python programming') # Process results for result in results: print(f"Rank: {result['rank']}") print(f"Title: {result['title']}") print(f"Abstract: {result['abstract']}") print(f"URL: {result['url']}") print("---") ``` ### Response #### Success Response (200) - **results** (list of dict) - A list of dictionaries, where each dictionary represents a search result. - **title** (string) - The title of the search result page. - **abstract** (string) - A snippet of the page content. - **url** (string) - The Baidu redirect URL for the search result. - **rank** (integer) - The position of the result in the search rankings (starting from 1). #### Response Example ```json [ { "rank": 1, "title": "Welcome to Python.org", "abstract": "The official home of the Python Programming Language...", "url": "http://www.baidu.com/link?url=cwYxPdTt2BvutAY8dyUXTmkaWD0dkOHxqdXx4Yf12cEz4QtxP20DS2V76sM02UiV" } ] ``` ``` -------------------------------- ### Result Data Structure Source: https://context7.com/amazingcoderpro/python-baidusearch/llms.txt Each search result is returned as a dictionary with four fields: `title`, `abstract`, `url`, and `rank`. ```APIDOC ## Result Data Structure ### Description Details the structure of each search result object returned by the `search` function. ### Fields Each search result is a dictionary containing the following fields: - **title** (string) - The title of the search result page. - **abstract** (string) - A snippet of the page content, typically up to 300 characters. - **url** (string) - The Baidu redirect URL for the search result. This URL should be used to access the actual webpage. - **rank** (integer) - The position of the result in the search rankings, starting from 1. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.