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

How to register for a free domain

How to register for a free domain  As when there is a need of website there will be a need of domain because domain will help the developers to promote their website to the internet users.As far as the domain is considered there are two types of domains we can register to host your website such as 1.Paid domain 2.Free domain In this article we will discuss how to register for the free domain in "Freenom" Steps to Register Domain: 1.Open any browser and type Freenom 2.Then type your required domain name and check for the available domains for your name 3.It will display all available free domains for your website name 4.Then select the required domain as per your requirements 5.Then select the days for the expiry of the domain and you can also forward the existing domain to the currently registered domain then click continue 6.Then enter your mail id for verfication and enter all the details. 7.Then Login to your account and select s

What skills are required to become an Ethical Hacker?

What skills are required to become an Ethical Hacker? Ethical hacking is a lucrative career choice that most people would like to pursue. As an ethical hacker, you can either be employed to work in organizations or you can provide your services as a freelancer. Internet and systems security are two things that are giving most organizations a headache. This directly means that your skills as an ethical hacker will always be in high demand. In this article, we will introduce you to the common programming languages and skills that you must know as a hacker. 1. Programming skills: All the websites and all types of software have been developed by different programming languages. Hacking is all about gaining access to the foundation of the software. In order to access this foundation, you need to have a proper understanding of the  Programming Languages that was used to develop the software. As an ethical hacker, you should have a proper grasp of different programming languages. This