-
-
Notifications
You must be signed in to change notification settings - Fork 48k
Update open_google_results.py #7085
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
Changes from 2 commits
4dc00a4
cb9661c
43c5fd6
e49f2dc
2db0177
5786bd1
296fe10
fb195cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,32 @@ | ||
import sys | ||
import webbrowser | ||
from sys import argv | ||
from urllib.parse import quote | ||
|
||
import requests | ||
from bs4 import BeautifulSoup | ||
from fake_useragent import UserAgent | ||
from requests import get | ||
|
||
if __name__ == "__main__": | ||
if len(argv) > 1: | ||
query = "%20".join(argv[1:]) | ||
else: | ||
query = quote(str(input("Search: "))) | ||
|
||
print("Googling.....") | ||
url = "https://www.google.com/search?q=" + " ".join(sys.argv[1:]) | ||
res = requests.get(url, headers={"UserAgent": UserAgent().random}) | ||
# res.raise_for_status() | ||
with open("project1a.html", "wb") as out_file: # only for knowing the class | ||
for data in res.iter_content(10000): | ||
out_file.write(data) | ||
soup = BeautifulSoup(res.text, "html.parser") | ||
links = list(soup.select(".eZt8xd"))[:5] | ||
|
||
print(len(links)) | ||
for link in links: | ||
if link.text == "Maps": | ||
webbrowser.open(link.get("href")) | ||
else: | ||
webbrowser.open(f"http://google.com{link.get('href')}") | ||
url = f"https://www.google.com/search?q={query}&num=2" | ||
|
||
res = get( | ||
url, | ||
headers={ | ||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0" | ||
|
||
}, | ||
) | ||
|
||
link = ( | ||
BeautifulSoup(res.text, "html.parser") | ||
.find("div", attrs={"class": "yuRUbf"}) | ||
.find("a") | ||
.get("href") | ||
) | ||
|
||
webbrowser.open(link) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A function called
get()
is not self-documenting and may confuse the reader ("Get what from where?") so lets userequests.get()
instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok sure!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from requests import getimport requests
Changed!