Skip to content

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

Merged
merged 8 commits into from
Oct 13, 2022
33 changes: 33 additions & 0 deletions web_programming/open_google_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import webbrowser
from sys import argv
from urllib.parse import quote

import requests
from bs4 import BeautifulSoup

if __name__ == "__main__":
if len(argv) > 1:
query = "%20".join(argv[1:])
else:
query = quote(str(input("Search: ")))

print("Googling.....")

url = f"https://www.google.com/search?q={query}&num=2"

res = requests.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)