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 3 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
7 changes: 6 additions & 1 deletion 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 @@ -39,4 +44,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)
```
```
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):
def _req(term, results, lang, start, proxies, timeout, safe):
resp = get(
url="https://www.google.com/search",
headers={
Expand All @@ -17,6 +17,7 @@ def _req(term, results, lang, start, proxies, timeout):
"num": results + 2, # Prevents multiple requests
"hl": lang,
"start": start,
"safe": safe,
},
proxies=proxies,
timeout=timeout,
Expand All @@ -35,7 +36,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", proxy=None, advanced=False, sleep_interval=0, timeout=5, safe="active"):
"""Search the Google search engine"""

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

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