Support
Please support this website. Visit the Amazon using this affiliate link. There won't be any difference in your purchage, we will get some commission for every purchase you make.
Advertise with us
script   3   10928
Python Script 4: Opening top 10 Google search result in one hit

This is the fourth script in 'Python Scripts' series.

You can have a look at previous scripts by following the links given below.

Python Script 3: Validate and format JSON string
Python Script 2: Crawling all emails from a website
Python Script 1: Convert ebook from one format to another

As the title suggests, this script is used to open the top 10 Google search results in different tabs for the provided keyword.



Code:

"""
Author: Anurag Rana
Usage: python google.py <keyword>
Description: Script googles the keyword and opens top 10(max) search results in separate tabs in the browser
Usage: python filename.py keyword
Tested with Python3
"""

import webbrowser
import sys
import pyperclip
import requests
import bs4


def start():
	if len(sys.argv) > 1:
		keyword = ' '.join(sys.argv[1:])
	else:
		# if no keyword is entered, the script would 
		# search for the keyword copied in the clipboard
		keyword = pyperclip.paste()

	res = requests.get('https://google.com/search?q='+keyword)
	soup = bs4.BeautifulSoup(res.text,'lxml')
	links = soup.select('.r a')
	tab_counts = min(10, len(links))

	for i in range(tab_counts):
		webbrowser.open('https://google.com' + links[i].get('href'))


start()
 

I am using this same concept at my work. Every day I have to open 8 applications (slack, JIRA, Gitlab, Facebook, StackOverflow, Jenkins UI, Outlook and Personal Email) in my browser. So instead of opening each application one by one, I just run the script and all these applications are opened in different tabs saving a few seconds.

script   3   10928
3 comments on 'Python Script 4: Opening Top 10 Google Search Result In One Hit'
Login to comment

Ranjan Oct. 20, 2019, 9:15 a.m.
after executing the program,i am getting no output.NO pages are being loaded.
Jay Soni Oct. 21, 2019, 1:13 p.m.
It's not work for me, i don't know why..because i don't get any error and any output but one thing i noticed here was if i write soup.select('a') instead of soup.select('.r a') it'll work but not properly, it means it only open tabs but they all are google tabs means google web,image,video,news etc but it can't open separate links in tabs.. here i try my best and i still do my best on this but it can't fix can you please help me?
Chris Dec. 4, 2019, 4:25 a.m.
I also am experiencing the exact same problem as Jay Soni. Please advise.

Related Articles:
Python program to convert Linux file permissions from octal number to rwx string
Python program to convert Linux file permissions from octal number to rwx string, Linux file conversion python program, Python script to convert Linux file permissions...
Python Script 17: Setting bing image of the day as desktop wallpaper
Python Script to Set bing image of the day as desktop wallpaper, Automating the desktop wallpaper change to bing image of day using python code, changing desktop wallpaper using python, downloading an image using python code, updating the desktop wallpaper daily using python...
Python Script 16: Generating word cloud image of a text using python
word cloud python, generating a word cloud of a text using python code, python code to generate a word cloud image of a text. Using word frequencies to generate a word cloud image using a python script...
Python Script 13: Generating ascii code from Image
Generating ascii art from image, converting colored image to ascii code, python script to convert image to ascii code, python code to generate the ascii image from jpg image....
DigitalOcean Referral Badge

© 2022-2023 Python Circle   Contact   Sponsor   Archive   Sitemap