{"id":130695,"date":"2022-12-20T11:15:35","date_gmt":"2022-12-20T11:15:35","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=991056"},"modified":"2022-12-20T11:15:35","modified_gmt":"2022-12-20T11:15:35","slug":"the-hidden-gems-4-best-google-search-libraries-for-python-you-cant-miss","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/12\/20\/the-hidden-gems-4-best-google-search-libraries-for-python-you-cant-miss\/","title":{"rendered":"The Hidden Gems: 4 Best Google Search Libraries for Python You Can\u2019t Miss!"},"content":{"rendered":"\n<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;}'>\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> <span class=\"kksr-muted\">Rate this post<\/span> <\/div>\n<\/p><\/div>\n<p>A few days ago, a coding friend asked me: <em><strong>\u201cWhat is the best Google search library for Python?\u201d<\/strong><\/em><\/p>\n<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>\n<p>So let\u2019s review some great libraries to access the powerful search of a Google query within your Python code.<\/p>\n<h2>Module 1: PyWhatKit<\/h2>\n<p>First, let\u2019s start simply by using the PyWhatKit Module:<\/p>\n<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>\n<p><strong>GitHub <\/strong>:\u00a0 <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/Ankit404butfound\/PyWhatKit\" target=\"_blank\">https:\/\/github.com\/Ankit404butfound\/PyWhatKit<\/a><\/p>\n<p>But you can simply use it as well to run your favorite Google Queries.<\/p>\n<p>So let\u2019s start with the basics!<\/p>\n<p>How to install it:<\/p>\n<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\u00a0<\/pre>\n<p>First to test your code:<\/p>\n<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\nprint(\"Let's ask Google!\")\nsearch1 = \"FIFA world Cup\"\npwk.search(search1)<\/pre>\n<p>Now a better code that asks for the user\u2019s <a href=\"https:\/\/blog.finxter.com\/python-input-function\/\" data-type=\"post\" data-id=\"24632\" target=\"_blank\" rel=\"noreferrer noopener\">input<\/a>:<\/p>\n<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\nfrom pywhatkit import search # Prompting for the user input query = input(\"Ask Google about? : \")\nprint(\"Searching for ...\")\n# Running the search query\nsearch(query)<\/pre>\n<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>\n<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\nfrom pywhatkit import search # Hardcoding your queries\nquery1 = \"Fifa\"\nquery2 = \"world cup\"\nquery3 = \"Mundial\"\nquery4 = \"world soccer\" # Searching at once\nsearch(query1)\nsearch(query2)\nsearch(query3)\nsearch(query4)<\/pre>\n<p>Ok, hold on! I can hear you saying but what about all Google search options. Well let\u2019s investigate another Python library: <code>google<\/code>!<\/p>\n<h2>Module 2: &#8216;Google&#8217;<\/h2>\n<p>To install it:<\/p>\n<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>\n<p>Now let&#8217;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>\n<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\nfrom 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): \"))\nsearchQueries = []\nwhile 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>\n<p>Let\u2019s have a look at those search function options:<\/p>\n<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\"\ntld = '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\u00a0 start = 0, # First result to retrieve\u00a0 stop = None, # Last result to retrieve\u00a0 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>\n<p>You will get some results in the following format (for example):<\/p>\n<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\nAsk Google about? : fifa<\/pre>\n<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\nhttps:\/\/www.tf1.fr\/tf1\/fifa-coupe-du-monde-de-football\/videos\/giroud-le-patient-anglais-91919296.html\nhttps:\/\/www.sports.fr\/football\/equipe-de-france\/classement-fifa-injustice-vue-bleus-672799.html\nhttps:\/\/www.fifa.com\/\nhttps:\/\/twitter.com\/FIFAcom\/status\/1600418112830115841?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Etweet<\/code>\n<\/pre>\n<h2>Module 3: Google-API-Python-Client<\/h2>\n<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>\n<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>\n<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>\n<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>\n<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>\n<p>Now we are ready to create a script:<\/p>\n<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\nimport json #we will need it for the output of course<\/pre>\n<p>Then, you will need to create two variables that contain your token and key to be authenticated by Google.<\/p>\n<p>For example, they may look as follows (do not copy them, they are fake <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f60a.png\" alt=\"\ud83d\ude0a\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>):<\/p>\n<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\"\nmy_cse_id = \"46c457999997\"\n\ufeff<\/pre>\n<p>Now let\u2019s create a function that we can call to do our search:<\/p>\n<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>\n<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>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n<p>Let\u2019s try our function to see if everything is working fine:<\/p>\n<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)\nprint(json.dumps(result, sort_keys=True, indent= 4))<\/pre>\n<p>Hold on, the output is not in a JSON file. No worries, let&#8217;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>\n<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)\njson_file = open(\"searchResult.json\", \"w\")\njson.dump(result, json_file)<\/pre>\n<p>Et voil\u00e0!<\/p>\n<h2>Module 4: SerpAPI<\/h2>\n<p>There is another option if you do not want to use the official Google API. <\/p>\n<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>\n<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\u00e9\", \"location\": \"France\", \"google_domain\": \"google.fr\", \"gl\": \"fr\", \"hl\": \"fr\", \"api_key\": \"cfc232bade8efdb3956XXXXXXxx02251b63f7c751b001a800b7c\"\n} search = GoogleSearch(params)\nresults = search.get_dict()<\/pre>\n<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>\n<p>I wanted to share this option as well, even though it is commercial. It can be useful for your use case.<\/p>\n<h2>Finishing Up<\/h2>\n<p>Some fun to finish for all the fans of Google out there? <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f60a.png\" alt=\"\ud83d\ude0a\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<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>\n<p>Please download any image by searching: <em><strong>\u201cI love Google\u201d<\/strong><\/em> and save it as a PNG.<\/p>\n<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\nimport pywhatkit as pwk pwk.image_to_ascii_art('loveGoogle.png', 'loveGascii.txt')<\/pre>\n<div class=\"wp-block-image\">\n<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\/uploads\/2022\/12\/image-265-300x178.png 300w\" sizes=\"auto, (max-width: 627px) 100vw, 627px\" \/><\/figure>\n<\/div>\n<p>Thanks for reading! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2665.png\" alt=\"\u2665\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rate this post A few days ago, a coding friend asked me: \u201cWhat is the best Google search library for Python?\u201d 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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-130695","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=130695"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130695\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=130695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=130695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=130695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}