Skip to main content

How to create a simple google search in python

How to create a simple google search in python



When you search for an information in google it displays the list of all results which is associated with your search.We can perform the similar task by programming using python within 10 lines of code.This is a fun part of programming in which we can create a simple google search within 10 lines of coding.

First of all fire up the terminal and check whether python3 is installed

Required libraries to perform google search in python

1.Requests
2.sys
3.webbrowser
4.bs4 (Beautiful Soup)

If any of these libraries are not installed do the following

If you had python3 then the command to install these packages are

pip3 install requests

pip3 install sys

pip3 install webbrowser

pip3 install bs4

Uses of these libraries:

Requests library is to request the search url from the user

sys.argv is a list in Python, which contains the command-line arguments passed to the script.

webbrowser is used to display the requested information to the user in the browser

Beautiful Soup is a Python library for pulling data out of HTML and XML file

Source code:

import requests,sys,webbrowser,bs4
res=requests.get('https://google.com/search?q='+''.join(sys.argv[1:]))
res.raise_for_status()
soup=bs4.BeautifulSoup(res.text,"html.parser")
linkElements=soup.select('.r a')
linkToOpen=min(10,len(linkElements))
for i in range(linkToOpen):
    webbrowser.open('https://google.com'+linkElements[i].get('href'))

Code Explanation:

1.Import all the required libraries which are mentioned above

2.Then request the user to input the search term in the command line argument so that we have used sys.argv to get the input in the command line.

3.This line checks the status of the search term and the result produced

4.This line initiates Beautiful soup which is retrieve all the html and xml files associated with the search term from the resultant sites.

5.This line uses regular expressions to separate the anchor tags which is link from all the websites which is associated with the users search term

6.This line tells how many link to open for the search term.You can any number of links to open for your search term.

7.Then loop over all the resultant links and then display it in the browser using the webbrowser library.

If you run the program by specifying the search term by

Python filename.py "search term" 


You should specify the search term inside the double quotation otherwise there will be a error.It will provide all the result associated with the search.

The output will be displayed in your default browser 

I hope you have enjoyed the post.Thanks for reading the post and please do share the post and make your valuable comments.


Comments

Popular posts from this blog

Top websites to learn programming and web development

Top websites to learn programming and web development Hey guys welcome to an another interesting post, In this post we will discuss about the top websites to learn programming and web development.As there are many websites available to learn programming but there is only few sites which provides an interactive way of learning and implementing in their own inbuilt compilers. As a learner it is recommended to visit these websites if you want to learn more about programing. This article gives you some basic information about some of those interactive websites that help you sharpen your existing knowledge on programming We will see some best sites to learn Python,java and web development which will be useful to learn and develop. Best websites for Python and Django: Python is a high level programming language which is popular now-a-days in industry.so there is a need to learn python to update from the current technologies so here is the list of all best website to learn python 1. L...

Microsoft Unveils New Open Source Programming Language

Microsoft Unveils New (and Simpler) Open Source Programming Language Microsoft Research has introduced a new open source programming language called  Bosque  that aspires to be simple and easy to understand by embracing algebraic operations and shunning techniques that create complexity. Bosque was inspired by the syntax and types of TypeScript and the semantics of  ML  and Node/JavaScript. It's the brainchild of Microsoft computer scientist Mark Marron, who describes the language as an effort to move beyond the structured programming model that became popular in the 1970s. Marron believes we can do better by getting rid of sources of complexity like loops, mutable state, and reference equality.  This model builds on the successes of structured programming and abstract data types by simplifying existing programming models into a regularized form that eliminates major sources of errors, simplifies code understanding and modification, an...

Top 7 Backend Web Frameworks To Use In 2019

Top 7 Backend Web Frameworks To Use In 2019 When it comes to web app development, frontend as well as the back end development, both play an important role in creating an amazing business website. The backend web frameworks consist of languages and tools used in server-side programming in a web application development environment. The key to the successful back end development of web applications is choosing the correct stack, learning new databases and programming languages and understanding the server architecture as well. Developers play a fundamental role in full stack web development as they create the central logic around which an application runs and dynamically integrates it with the front-end. However, there are few things to be considered while choosing the top backend web frameworks for your next full stack web application development project. - Ease-of-use - Speed - Documentation - Community In this Post, we will discuss some top backend web frameworks for back end...