Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add information about non-qdr options?

```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
Expand All @@ -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)
```
```
21 changes: 12 additions & 9 deletions googlesearch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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.
Expand All @@ -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")
Expand Down