Created
March 27, 2021 18:34
-
-
Save im-noob/e98023aff64b258248d625e5839d19ee to your computer and use it in GitHub Desktop.
Find First Url from Google Search
This file contains hidden or 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
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