Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] The Hidden Gems: 4 Best Google Search Libraries for Python You Can’t Miss!

#1
The Hidden Gems: 4 Best Google Search Libraries for Python You Can’t Miss!

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;991056&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 0px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> <span class="kksr-muted">Rate this post</span> </div>
</p></div>
<p>A few days ago, a coding friend asked me: <em><strong>“What is the best Google search library for Python?”</strong></em></p>
<p>Well, to be honest, I had no clue, so I did my investigation and did some quick testing. And I thought that it might be useful to share it with some Pythonistas newbies out of there.</p>
<p>So let’s review some great libraries to access the powerful search of a Google query within your Python code.</p>
<h2>Module 1: PyWhatKit</h2>
<p>First, let’s start simply by using the PyWhatKit Module:</p>
<p class="has-global-color-8-background-color has-background">PyWhatKit is a Python Library that allows you to schedule and send WhatsApp messages and perform other functions such as playing a video on YouTube, converting an image to ASCII art, and converting a string to an image with handwritten characters. Other features include sending emails, taking screenshots, and shutting down or canceling a shutdown on a Linux or Mac OS machine.</p>
<p><strong>GitHub </strong>:  <a rel="noreferrer noopener" href="https://github.com/Ankit404butfound/PyWhatKit" target="_blank">https://github.com/Ankit404butfound/PyWhatKit</a></p>
<p>But you can simply use it as well to run your favorite Google Queries.</p>
<p>So let’s start with the basics!</p>
<p>How to install it:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install pywhatkit </pre>
<p>First to test your code:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pywhatkit as pwk # To perform a Google search and to open your default browser automatically
print("Let's ask Google!")
search1 = "FIFA world Cup"
pwk.search(search1)</pre>
<p>Now a better code that asks for the user’s <a href="https://blog.finxter.com/python-input-function/" data-type="post" data-id="24632" target="_blank" rel="noreferrer noopener">input</a>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Importing the search function from the pywhatkit library
from pywhatkit import search # Prompting for the user input query = input("Ask Google about? : ")
print("Searching for ...")
# Running the search query
search(query)</pre>
<p>You can run multiple queries, but this will open as many tabs in your browser. Maybe you have a use case for that?</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># To import the search function from the pywhatkit library
from pywhatkit import search # Hardcoding your queries
query1 = "Fifa"
query2 = "world cup"
query3 = "Mundial"
query4 = "world soccer" # Searching at once
search(query1)
search(query2)
search(query3)
search(query4)</pre>
<p>Ok, hold on! I can hear you saying but what about all Google search options. Well let’s investigate another Python library: <code>google</code>!</p>
<h2>Module 2: ‘Google’</h2>
<p>To install it:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install google</pre>
<p>Now let’s ask the user what is looking for and return the result of all queries with a list of URLs. More useful when doing an investigation.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Importing the search function from the google library
from googlesearch import search # Asking the user how many queries he wants to run num_searches = int(input("How many queries do you to do, (ie: 3): "))
searchQueries = []
while num_searches>0: # Then asking the user the subject of the search query query = input("Ask Google about? : ") # This will display a max of 5 results per query for i in search(query, tld="com", num=5, stop=5, pause=3): print(i) num_searches=num_searches-1</pre>
<p>Let’s have a look at those search function options:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">query # a string which contain what you are looking for; ie: "FIFA World cup"
tld = 'com', # The top level domain of google; ie: 'co.uk, fr' , ... lang = 'en', # Language of search result, 'ie: fr=French; gr=german',... num = 10, # Number of results per page  start = 0, # First result to retrieve  stop = None, # Last result to retrieve  pause = 2.0, # Lapse between HTTP requests, this is important because if this value is too low Google can block your IP, so I recommend 2 or 3 seconds</pre>
<p>You will get some results in the following format (for example):</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">How many queries do you to do, (ie: 3): 1
Ask Google about? : fifa</pre>
<pre class="wp-block-preformatted"><code>https://www.beinsports.com/france/fifa-coupe-du-monde-2022/video/coupe-du-monde-2022-lenorme-occasion-pour-le-/2000861
https://www.tf1.fr/tf1/fifa-coupe-du-mon...19296.html
https://www.sports.fr/football/equipe-de...72799.html
https://www.fifa.com/
https://twitter.com/FIFAcom/status/16004...weet</code>
</pre>
<h2>Module 3: Google-API-Python-Client</h2>
<p>Now, if you are interested in getting your results back in a JSON format, then the solution is to use the official Google API.</p>
<p>Before using the Google Python library, you need to create a Google account and get your API key <a rel="noreferrer noopener" href="https://cloud.google.com/" data-type="URL" data-id="https://cloud.google.com/" target="_blank">here</a>. You will need to create as well a service account in the <a rel="noreferrer noopener" href="https://search.google.com/search-console/about" data-type="URL" data-id="https://search.google.com/search-console/about" target="_blank">Google Search Console</a>.</p>
<p>I do recommend reading <a rel="noreferrer noopener" href="https://blog.opstree.com/2022/04/26/google-python-api-the-easy-way/" data-type="URL" data-id="https://blog.opstree.com/2022/04/26/google-python-api-the-easy-way/" target="_blank">this blog</a> if you are struggling to create your account and keys.</p>
<p>For the <a href="https://blog.finxter.com/how-to-install-google-api-python-client-in-python/" data-type="post" data-id="35874">installat</a><a href="https://blog.finxter.com/how-to-install-google-api-python-client-in-python/" data-type="post" data-id="35874" target="_blank" rel="noreferrer noopener">i</a><a href="https://blog.finxter.com/how-to-install-google-api-python-client-in-python/" data-type="post" data-id="35874">on</a> of the library:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install google-api-python-client</pre>
<p>Now we are ready to create a script:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from googleapiclient.discovery import build #Import the Google API library
import json #we will need it for the output of course</pre>
<p>Then, you will need to create two variables that contain your token and key to be authenticated by Google.</p>
<p>For example, they may look as follows (do not copy them, they are fake <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f60a.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />):</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">my_api_key = "AIzaSyAezbZKKKKKKr56r8kZk"
my_cse_id = "46c457999997"
</pre>
<p>Now let’s create a function that we can call to do our search:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def search(search_term, api_key, cse_id, **kwargs): service = build("customsearch", "v1", developerKey=api_key) result = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute() return result</pre>
<p>In the second line, we are using the <code>build()</code> command, which has many options because this Google API can be used for many things, but the one we are interested here is the: <code>customsearch</code>.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Resources</strong>: More information on all Google services is available <a rel="noreferrer noopener" href="https://github.com/googleapis/google-api-python-client/blob/main/docs/dyn/index.md" data-type="URL" data-id="https://github.com/googleapis/google-api-python-client/blob/main/docs/dyn/index.md" target="_blank">here</a>. More information on the <code>build</code> command <a rel="noreferrer noopener" href="https://googleapis.github.io/google-api-python-client/docs/epy/googleapiclient.discovery-module.html#build" data-type="URL" data-id="https://googleapis.github.io/google-api-python-client/docs/epy/googleapiclient.discovery-module.html#build" target="_blank">here</a>.</p>
<p>Let’s try our function to see if everything is working fine:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">result = search("FIFA", my_api_key, my_cse_id)
print(json.dumps(result, sort_keys=True, indent= 4))</pre>
<p>Hold on, the output is not in a JSON file. No worries, let’s modify the last bit so you can <a href="https://blog.finxter.com/how-to-create-and-work-with-a-json-file-in-python/" data-type="post" data-id="852705" target="_blank" rel="noreferrer noopener">save it</a> directly to a JSON file:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">result = search("FIFA", my_api_key, my_cse_id)
json_file = open("searchResult.json", "w")
json.dump(result, json_file)</pre>
<p>Et voilà!</p>
<h2>Module 4: SerpAPI</h2>
<p>There is another option if you do not want to use the official Google API. </p>
<p>The <code>google-search-results</code> library is not free for unlimited searches. But it can be free for 100 searches per day if you create an account at <code>SerpApi.com</code>, and retrieve your API Key can add it to your code as you can guess!</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from serpapi import GoogleSearch params = { "device": "desktop", "engine": "google", "q": "café", "location": "France", "google_domain": "google.fr", "gl": "fr", "hl": "fr", "api_key": "cfc232bade8efdb3956XXXXXXxx02251b63f7c751b001a800b7c"
} search = GoogleSearch(params)
results = search.get_dict()</pre>
<p>You will get a nice <a href="https://blog.finxter.com/how-to-create-and-work-with-a-json-file-in-python/" data-type="post" data-id="852705">JSON file</a> as a result. This library might be interesting for you if you want to do an internet search on other engines. <code>Serapi.com</code> supports Bing, DuckDuckGo, Yahoo, Yandex, eBay, Youtube, and many more.</p>
<p>I wanted to share this option as well, even though it is commercial. It can be useful for your use case.</p>
<h2>Finishing Up</h2>
<p>Some fun to finish for all the fans of Google out there? <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f60a.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>By using the first library that we play with, you can transform any images into an <a href="https://blog.finxter.com/ascii-table/" data-type="post" data-id="17877" target="_blank" rel="noreferrer noopener">ASCII</a> art file, special memories for the 80s.</p>
<p>Please download any image by searching: <em><strong>“I love Google”</strong></em> and save it as a PNG.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># To import the search function from the pywhatkit library
import pywhatkit as pwk pwk.image_to_ascii_art('loveGoogle.png', 'loveGascii.txt')</pre>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="627" height="372" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-265.png" alt="" class="wp-image-991079" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-265.png 627w, https://blog.finxter.com/wp-content/uplo...00x178.png 300w" sizes="(max-width: 627px) 100vw, 627px" /></figure>
</div>
<p>Thanks for reading! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2665.png" alt="♥" class="wp-smiley" style="height: 1em; max-height: 1em;" /> </p>
</div>


https://www.sickgaming.net/blog/2022/12/...cant-miss/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016