Skip to content

Commit fb195cc

Browse files
authored
Update open_google_results.py
1 parent 296fe10 commit fb195cc

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed
Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import webbrowser
22
from sys import argv
3-
from urllib.parse import quote
3+
from urllib.parse import quote, parse_qs
4+
from fake_useragent import UserAgent
45

56
import requests
67
from bs4 import BeautifulSoup
@@ -13,21 +14,29 @@
1314

1415
print("Googling.....")
1516

16-
url = f"https://www.google.com/search?q={query}&num=2"
17+
url = f"https://www.google.com/search?q={query}&num=100"
1718

1819
res = requests.get(
1920
url,
2021
headers={
21-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) "
22-
"Gecko/20100101 Firefox/98.0"
22+
"User-Agent": str(UserAgent().random)
2323
},
2424
)
2525

26-
link = (
27-
BeautifulSoup(res.text, "html.parser")
28-
.find("div", attrs={"class": "yuRUbf"})
29-
.find("a")
30-
.get("href")
31-
)
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]
3241

3342
webbrowser.open(link)

0 commit comments

Comments
 (0)