yagooglesearch

repository·master·Indexed 43 days ago

https://github.com/opsdisk/yagooglesearch

A Python library for executing intelligent, realistic-looking, and tunable Google searches. It features a SearchClient to simulate human behavior, avoid HTTP 429 rate limits through automatic recovery mechanisms, and supports HTTP and SOCKS5 proxies. The library allows for detailed search configuration, including TLD, language, and time-based filters via the tbs parameter and get_tbs helper function.

Tokens
2.6K
Snippets
8
Records
12
Agent score
55%

What's inside yagooglesearch

  1. Handle HTTP 429 rate limiting

    master

    By default, yagooglesearch automatically handles HTTP 429 (Too Many Requests) errors by sleeping for http_429_cool_off_time_in_minutes and increasing the wait time by http_429_cool_off_factor on subsequent detections.

    If you prefer to manage rate limiting manually, set yagooglesearch_manages_http_429s=False. In this mode, if a 429 is detected, the string "HTTP_429_DETECTED" is appended to the returned list of results. You must check for this string and handle it (e.g., by removing it from the list) to process the URLs found before the block.

    import yagooglesearch
    
    client = yagooglesearch.SearchClient(
        "site:twitter.com",
        tbs="li:1",
        verbosity=4,
        num=10,
        max_search_result_urls_to_return=1000,
        minimum_delay_between_paged_results_in_seconds=1,
        yagooglesearch_manages_http_429s=False,  # Manual management
    )
    client.assign_random_user_agent()
    
    urls = client.search()
    
    if "HTTP_429_DETECTED" in urls:
        print("HTTP 429 detected... it's up to you to modify your search.")
        urls.remove("HTTP_429_DETECTED")
        for url in urls:
            print(url)
  2. Install yagooglesearch

    master

    You can install yagooglesearch via pip or by cloning the repository and installing from source using pyproject.toml.

    # Using pip
    pip install yagooglesearch
    
    # From source
    git clone https://github.com/opsdisk/yagooglesearch
    cd yagooglesearch
    virtualenv -p python3 .venv
    source .venv/bin/activate
    pip install .
  3. Handle HTTP 429 (Rate Limiting) in SearchClient

    master

    Google may block your IP if too many requests are made. SearchClient provides two ways to handle this:

    1. Automatic Management (Default): Set yagooglesearch_manages_http_429s=True. When a 429 is detected, the client will sleep for http_429_cool_off_time_in_minutes and then retry. Each subsequent detection increases the sleep time by the http_429_cool_off_factor.
    2. Manual Management: Set yagooglesearch_manages_http_429s=False. When a 429 is detected, the search() method will immediately return a list containing the single string "HTTP_429_DETECTED". You can then decide how to pause or rotate proxies in your own code.
  4. Rotate multiple proxies

    master

    To use multiple proxies, you must manually instantiate a new SearchClient for each proxy in your rotation loop.

    import yagooglesearch
    
    proxies = [
        "socks5h://127.0.0.1:9050",
        "socks5h://127.0.0.1:9051",
        "http://127.0.0.1:9052",
    ]
    
    search_queries = ["python", "site:github.com pagodo"]
    proxy_rotation_index = 0
    
    for search_query in search_queries:
        proxy_index = proxy_rotation_index % len(proxies)
        client = yagooglesearch.SearchClient(
            search_query,
            proxy=proxies[proxy_index],
        )
    
        if proxies[proxy_index].startswith("http://"):
            client.verify_ssl = False
    
        urls_list = client.search()
        print(urls_list)
        proxy_rotation_index += 1
  5. Basic usage of SearchClient

    master

    To perform a Google search, instantiate a yagooglesearch.SearchClient with your query and desired parameters, then call .search(). It is highly recommended to call .assign_random_user_agent() to help avoid rate limiting.

    import yagooglesearch
    
    query = "site:github.com"
    
    client = yagooglesearch.SearchClient(
        query,
        tbs="li:1",
        max_search_result_urls_to_return=100,
        http_429_cool_off_time_in_minutes=45,
        http_429_cool_off_factor=1.5,
        # proxy="socks5h://127.0.0.1:9050",
        verbosity=5,
        verbose_output=True,  # False (only URLs) or True (rank, title, description, and URL)
    )
    client.assign_random_user_agent()
    
    urls = client.search()
    
    for url in urls:
        print(url)
  6. Configure HTTP and SOCKS5 proxies

    master

    You can provide a proxy string during SearchClient initialization. The library supports http, https, socks5, and socks5h schemes.

    Recommendation: Use socks5h if you are using SOCKS to ensure DNS resolution happens on the proxy server rather than locally.

    client = yagooglesearch.SearchClient(
        "site:github.com",
        proxy="socks5h://127.0.0.1:9050",
    )
  7. Disable SSL/TLS verification for HTTPS proxies

    master

    If you are using an HTTPS proxy with a self-signed certificate, you must disable SSL verification. This can be done during instantiation via verify_ssl=False or by setting the attribute on the client instance after creation.

    # Option 1: During instantiation
    client = yagooglesearch.SearchClient(
        query="site:github.com",
        proxy="http://127.0.0.1:8080",
        verify_ssl=False,
        verbosity=5,
    )
    
    # Option 2: After instantiation
    client = yagooglesearch.SearchClient(query="site:github.com", proxy="http://127.0.0.1:8080")
    client.verify_ssl = False
  8. Configure SearchClient HTTP 429 recovery parameters

    master

    When yagooglesearch_manages_http_429s is enabled, you can tune the recovery behavior using these parameters in the SearchClient constructor:

    • http_429_cool_off_time_in_minutes: The initial sleep duration in minutes (e.g., 60).
    • http_429_cool_off_factor: The multiplier applied to the sleep time for every consecutive 429 error (e.g., 1.1).
  9. Use &tbs= URL filters

    master

    The tbs parameter allows you to apply verbatim or time-based filters to your search.

    Verbatim search: Use tbs="li:1".

    Time-based filters:

    Time filter&tbs= parameterNotes
    Past hourqdr:h
    Past dayqdr:dPast 24 hours
    Past weekqdr:w
    Past monthqdr:m
    Past yearqdr:y
    Customcdr:1,cd_min:1/1/2021,cd_max:6/1/2021Use yagooglesearch.get_tbs() for assistance
  10. Initialize and use the SearchClient

    master

    The SearchClient class is the primary interface for performing Google searches. You can configure various parameters such as TLD, language, search safety, and proxy settings.

    By default, search() returns a list of URLs. If you set verbose_output=True, it returns a list of dictionaries containing rank, title, description, and url.

    Key Configuration Options:

    • query: The search string (do not URL-encode it manually).
    • tld: Top-level domain (e.g., com).
    • lang_result: The language of the search results (e.g., lang_en).
    • tbs: Time-based search parameters (e.g., qdr:h for the last hour).
    • max_search_result_urls_to_return: Limits the total number of URLs collected.
    • verbose_output: Set to True to get detailed metadata instead of just URLs.
    • proxy: A string for HTTP(S) or SOCKS5 proxies.
    • yagooglesearch_manages_http_429s: If True (default), the client handles rate-limiting by sleeping and retrying. If False, it returns the string "HTTP_429_DETECTED" when blocked.
    from yagooglesearch import SearchClient
    
    # Basic usage (returns list of URLs)
    client = SearchClient(query="python programming")
    results = client.search()
    print(results)
    
    # Detailed usage (returns list of dicts with metadata)
    client = SearchClient(
        query="python programming",
        verbose_output=True,
        max_search_result_urls_to_return=20
    )
    detailed_results = client.search()
    for res in detailed_results:
        print(f"{res['rank']}: {res['title']} - {res['url']}")
  11. Format custom date ranges with get_tbs

    master

    The get_tbs helper function converts Python datetime.date objects into the specific tbs string format required by Google for custom date range searches (cdr:1,cd_min:MM/DD/YYYY,cd_max:MM/DD/YYYY).

    Use this function to populate the tbs parameter in the SearchClient constructor.

    from datetime import date
    from yagooglesearch import SearchClient, get_tbs
    
    from_date = date(2021, 1, 1)
    to_date = date(2021, 6, 1)
    tbs_param = get_tbs(from_date, to_date)
    
    client = SearchClient(query="search term", tbs=tbs_param)
    results = client.search()