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 feature to change headers if needed #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
27 changes: 13 additions & 14 deletions googlesearch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
from .user_agents import get_useragent


def _req(term, results, lang, start, proxies, timeout, safe, ssl_verify, region):
def _req(term, results, lang, start, proxies, timeout, safe, ssl_verify, region,*,headers=None):
"""If headers=none, the headers will only have a random common user agent. Use headers={} for no headers (not recommended)"""
if not headers: # if headers is None or unset...
headers={
"User-Agent": get_useragent() #set it to user agent
}
resp = get(
url="https://www.google.com/search",
headers={
"User-Agent": get_useragent()
},
headers=headers,
params={
"q": term,
"num": results + 2, # Prevents multiple requests
Expand All @@ -36,21 +39,21 @@ def __init__(self, url, title, description):
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, safe="active", ssl_verify=None, region=None, start_num=0, unique=False):
"""Search the Google search engine"""
def search(term, num_results=10, lang="en", proxy=None, advanced=False, sleep_interval=0, timeout=5, safe="active", ssl_verify=None, region=None,headers=None):
"""Search the Google search engine.
If headers=none, the headers will only have a random common user agent. Use headers={} for no headers (not recommended).
>>> search("python",num_results=10,lang="en")"""

# Proxy setup
proxies = {"https": proxy, "http": proxy} if proxy and (proxy.startswith("https") or proxy.startswith("http")) else None

start = start_num
start = 0
fetched_results = 0 # Keep track of the total fetched results
fetched_links = set() # to keep track of links that are already seen previously

while fetched_results < num_results:
# Send request
resp = _req(term, num_results - start,
lang, start, proxies, timeout, safe, ssl_verify, region)
lang, start, proxies, timeout, safe, ssl_verify, region,headers=headers)

# Parse
soup = BeautifulSoup(resp.text, "html.parser")
Expand All @@ -64,10 +67,6 @@ def search(term, num_results=10, lang="en", proxy=None, advanced=False, sleep_in
description_box = result.find("div", {"style": "-webkit-line-clamp:2"})

if link and title and description_box:
link = result.find("a", href=True)
Copy link
Owner

Choose a reason for hiding this comment

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

Why did you remove this part?

if link["href"] in fetched_links and unique:
continue
fetched_links.add(link["href"])
description = description_box.text
fetched_results += 1
new_results += 1
Expand Down