Skip to content

Commit f176786

Browse files
cj-praveenpre-commit-ci[bot]cclauss
authored
Update open_google_results.py (TheAlgorithms#7085)
* update crawl_google_results.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update and rename crawl_google_results.py to open_google_results.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Create crawl_google_results.py * Update web_programming/open_google_results.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update open_google_results.py Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent d5a9f64 commit f176786

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import webbrowser
2+
from sys import argv
3+
from urllib.parse import quote, parse_qs
4+
from fake_useragent import UserAgent
5+
6+
import requests
7+
from bs4 import BeautifulSoup
8+
9+
if __name__ == "__main__":
10+
if len(argv) > 1:
11+
query = "%20".join(argv[1:])
12+
else:
13+
query = quote(str(input("Search: ")))
14+
15+
print("Googling.....")
16+
17+
url = f"https://www.google.com/search?q={query}&num=100"
18+
19+
res = requests.get(
20+
url,
21+
headers={
22+
"User-Agent": str(UserAgent().random)
23+
},
24+
)
25+
26+
try:
27+
link = (
28+
BeautifulSoup(res.text, "html.parser")
29+
.find("div", attrs={"class": "yuRUbf"})
30+
.find("a")
31+
.get("href")
32+
)
33+
34+
except AttributeError:
35+
link = parse_qs(
36+
BeautifulSoup(res.text, "html.parser")
37+
.find("div", attrs={"class": "kCrYT"})
38+
.find("a")
39+
.get("href")
40+
)["url"][0]
41+
42+
webbrowser.open(link)

0 commit comments

Comments
 (0)