Skip to content

Instantly share code, notes, and snippets.

@im-noob
Created March 27, 2021 18:34
Show Gist options
  • Save im-noob/e98023aff64b258248d625e5839d19ee to your computer and use it in GitHub Desktop.
Save im-noob/e98023aff64b258248d625e5839d19ee to your computer and use it in GitHub Desktop.
Find First Url from Google Search
import requests
from bs4 import BeautifulSoup
import time
search = input('Search: ')
searched_url = 'https://www.google.com/search?q='+search.replace(' ','+')
# googling
page = requests.get(searched_url)
soup = BeautifulSoup(page.content, 'html.parser')
all_a_tag = soup.find_all('a')
for one_a in all_a_tag:
if one_a['href'].find('/url?q=') == 0:
replaced_url = one_a['href'].replace('/url?q=','')
final_url = replaced_url.split('&sa=U&ved=')[0]
print(final_url)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment