forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen_google_results.py
More file actions
Latest commit
39 lines (30 loc) · 900 Bytes
/
Copy pathopen_google_results.py
File metadata and controls
39 lines (30 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "beautifulsoup4",
# "fake-useragent",
# "httpx",
# ]
# ///
importwebbrowser
fromsysimportargv
fromurllib.parseimportparse_qs, quote
importhttpx
frombs4importBeautifulSoup
fromfake_useragentimportUserAgent
if__name__=="__main__":
query="%20".join(argv[1:]) iflen(argv) >1elsequote(str(input("Search: ")))
print("Googling.....")
url=f"https://www.google.com/search?q={query}&num=100"
res=httpx.get(
url,
headers={"User-Agent": str(UserAgent().random)},
timeout=10,
)
try:
link=BeautifulSoup(res.text, "html.parser").find("div").find("a").get("href")
exceptAttributeError:
link=parse_qs(
BeautifulSoup(res.text, "html.parser").find("div").find("a").get("href")
)["url"][0]
webbrowser.open(link)