Skip to content

Commit

Permalink
Merge pull request #76 from VincentX0905/master
Browse files Browse the repository at this point in the history
Add Safe Search
  • Loading branch information
Nv7-GitHub authored Apr 27, 2024
2 parents e27f481 + 9bc2dc4 commit 2ea17d2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ In addition, you can change the language google searches in. For example, to get
from googlesearch import search
search("Google", lang="fr")
```
If you want to turn off the safe search function (this function is on by default), you can do this:
```python
from googlesearch import search
search("Google", safe=None)
```
To extract more information, such as the description or the result URL, use an advanced search:
```python
from googlesearch import search
Expand All @@ -40,6 +45,7 @@ If requesting more than 100 results, googlesearch will send multiple requests to
from googlesearch import search
search("Google", sleep_interval=5, num_results=200)
```

If you are using a HTTP Rotating Proxy which requires you to install their CA Certificate, you can simply add `SSL_VERIFY=False` in the `search()` method to avoid SSL Verification.
```python
from googlesearch import search
Expand Down
7 changes: 4 additions & 3 deletions googlesearch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import urllib


def _req(term, results, lang, start, proxies, timeout, SSL_VERIFY):
def _req(term, results, lang, start, proxies, timeout, safe, SSL_VERIFY):
resp = get(
url="https://www.google.com/search",
headers={
Expand All @@ -17,6 +17,7 @@ def _req(term, results, lang, start, proxies, timeout, SSL_VERIFY):
"num": results + 2, # Prevents multiple requests
"hl": lang,
"start": start,
"safe": safe,
},
proxies=proxies,
timeout=timeout,
Expand All @@ -36,7 +37,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, SSL_VERIFY=None):
def search(term, num_results=10, lang="en", proxy=None, advanced=False, sleep_interval=0, timeout=5, safe="active", SSL_VERIFY=None):
"""Search the Google search engine"""

escaped_term = urllib.parse.quote_plus(term) # make 'site:xxx.xxx.xxx ' works.
Expand All @@ -54,7 +55,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, SSL_VERIFY)
lang, start, proxies, timeout, safe, SSL_VERIFY)

# Parse
soup = BeautifulSoup(resp.text, "html.parser")
Expand Down

0 comments on commit 2ea17d2

Please sign in to comment.