serpapi / google-search-results-python

Google Search Results via SERP API pip Python Package
MIT License
600 stars 97 forks source link

Setting Timeout on Instantiation for GoogleSearch #40

Open zach-two opened 1 year ago

zach-two commented 1 year ago

Currently it is not possible to set a timeout threshold on Instantiation of a GoogleSearch client and instead it defaults to a hard-coded value of 60,000.

ilyazub commented 1 year ago

It's possible to tweak the timeout by using the SerpApiClient. Example project in web-IDE.

params = {
    "engine": "google_maps",
    "q": q,
    "type": "search",
    "ll": "@40.7455096,-74.0083012,14z",
    "api_key": os.getenv("SERPAPI_KEY"),
}

# Timeout is set to 1 ms to illustrate that it works.
# In a real project, some searches can take up to 450 seconds on the SerpApi side:
# Reason: parameters complexity and other factors on target websites' side.
# Example: SerpApiClient(params, timeout=90)
search = SerpApiClient(params, timeout=1)

Outputs:

requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='serpapi.com', port=443): Read timed out. (read timeout=1)

@zach-two, what do you think about the proposed solution?

zach-two commented 1 year ago

Can you please clarify if the timeout units are seconds or milliseconds? Since in the underlying implementation the timeout is passed to the get method of requests and the docs mention it is in seconds.

ilyazub commented 1 year ago

It's in seconds, according to requests source code and urllib code documentation. (I've updated the code sample above to reflect that timeout is in seconds.)