Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Safe Search #76

Merged
merged 4 commits into from
Apr 27, 2024
Merged
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
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