python-baidusearch

repository·master·Indexed 41 days ago

https://github.com/amazingcoderpro/python-baidusearch

An unofficial, free, and unrestricted Python wrapper for the Baidu Search engine. It uses web scraping to provide search results without requiring API keys, supporting both Python 2 and 3. It provides a search() function returning titles, abstracts, and URLs, and can be used as a CLI tool.

Tokens
735
Snippets
4
Records
5
Agent score
1%

What's inside python-baidusearch

  1. Avoid IP blocking when using the Baidu Search API

    master

    Baidu Search servers may block your IP if you overuse the library. To minimize the risk of being blocked:

    • Implement delays: It is recommended to maintain a gap of at least 15 seconds between consecutive searches.
    • Handle rate limits: If you encounter a 503 error or a 'rate limit' message, stop all requests immediately and wait for approximately 1 minute before retrying.
    • Note on browser behavior: Even if searches via a web browser continue to work, the library may stop functioning if your IP is flagged for automated usage.
  2. Perform a Baidu search using the search() function

    master

    The search function allows you to perform queries on Baidu. It returns a list of dictionaries, where each dictionary represents a search result containing the title, abstract, url, and rank.

    Parameters:

    • query (str): The search term.
    • num_results (int, optional): The number of results to return.

    Return Value: A list of dictionaries. Example structure:

    {
      'title': '...', 
      'abstract': '...', 
      'url': '...', 
      'rank': 1
    }
    from baidusearch.baidusearch import search
    
    # Basic search
    results = search('Python')
    
    # Search with specific number of results
    results = search('Baidu Search API', num_results=15)
  3. Use the search function in Python

    master

    Import the search function from baidusearch.baidusearch to perform web searches. The function returns a list of dictionaries containing search results.

    Parameters:

    • query (str): The search term.
    • num_results (int, optional): The maximum number of results to return.

    Return Format: A list of dictionaries, where each dictionary contains:

    • title: The title of the result.
    • abstract: A short snippet or description of the result.
    • url: The URL of the result.
    from baidusearch.baidusearch import search
    
    # Returns 10 or less results by default
    results = search('Full Stack Developer')
    
    # Returns up to 20 results
    results = search('China', num_results=20)
    
    # Example output structure:
    # [
    #   {"title": "Name", "abstract": "Snippet", "url": "http://..."},
    #   ...
    # ]