diff --git a/README.md b/README.md index e43f1bc..6d61bb2 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,16 @@ In addition, you can change the language google searches in. For example, to get from googlesearch import search search("Google", lang="fr") ``` + +### Time-Based Search (tbs) Parameter +You can also refine search results based on time. The tbs parameter allows you to filter results by various time ranges. For instance, to get results from the past hour, use `qdr:h`, or for results from the past year, use `qdr:y`. Here's how you can use the tbs parameter: +```python +from googlesearch import search +search("Google", tbs="qdr:h") # Results from the past hour +search("Google", tbs="qdr:m") # Results from the past month +search("Google", tbs="qdr:y") # Results from the past year +``` + To extract more information, such as the description or the result URL, use an advanced search: ```python from googlesearch import search @@ -39,4 +49,4 @@ If requesting more than 100 results, googlesearch will send multiple requests to ```python from googlesearch import search search("Google", sleep_interval=5, num_results=200) -``` \ No newline at end of file +``` diff --git a/googlesearch/__init__.py b/googlesearch/__init__.py index 74e6564..c7b5057 100644 --- a/googlesearch/__init__.py +++ b/googlesearch/__init__.py @@ -6,18 +6,21 @@ import urllib -def _req(term, results, lang, start, proxies, timeout): +def _req(term, results, lang, start, tbs, proxies, timeout): + params={ + "q": term, + "num": results + 2, # Prevents multiple requests + "hl": lang, + "start": start, + } + if tbs is not None: + params["tbs"] = tbs resp = get( url="https://www.google.com/search", headers={ "User-Agent": get_useragent() }, - params={ - "q": term, - "num": results + 2, # Prevents multiple requests - "hl": lang, - "start": start, - }, + params=params, proxies=proxies, timeout=timeout, ) @@ -35,7 +38,7 @@ def __repr__(self): return f"SearchResult(url={self.url}, title={self.title}, description={self.description})" -def search(term, num_results=10, lang="en", proxy=None, advanced=False, sleep_interval=0, timeout=5): +def search(term, num_results=10, lang="en", tbs=None, proxy=None, advanced=False, sleep_interval=0, timeout=5): """Search the Google search engine""" escaped_term = urllib.parse.quote_plus(term) # make 'site:xxx.xxx.xxx ' works. @@ -53,7 +56,7 @@ def search(term, num_results=10, lang="en", proxy=None, advanced=False, sleep_in while start < num_results: # Send request resp = _req(escaped_term, num_results - start, - lang, start, proxies, timeout) + lang, start, tbs, proxies, timeout) # Parse soup = BeautifulSoup(resp.text, "html.parser")