Created
August 19, 2024 11:58
-
-
Save Ektaynot/46681539aa1c030b3a58986e7f3df397 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Written by ChatGPT | |
import requests | |
from bs4 import BeautifulSoup | |
import argparse | |
import pyperclip | |
parser = argparse.ArgumentParser(description='Get the first url from a google search.') | |
parser.add_argument('-w', '--word', type=str, help='Word') | |
args = parser.parse_args() | |
word = getattr(args, 'word').lower() | |
def get_first_search_result(query): | |
url = f"https://www.google.com/search?q={query}" | |
headers = { | |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" | |
} | |
try: | |
response = requests.get(url, headers=headers) | |
response.raise_for_status() | |
soup = BeautifulSoup(response.text, 'html.parser') | |
search_results = soup.find_all('div', class_='tF2Cxc') | |
if search_results: | |
first_result = search_results[0].find('a')['href'] | |
return first_result | |
else: | |
return "No results found." | |
except requests.exceptions.RequestException as e: | |
return f"Error: {e}" | |
if __name__ == "__main__": | |
first_result = get_first_search_result(word) | |
print("First search result:", first_result) | |
pyperclip.copy(first_result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment