{ "cells": [ { "cell_type": "markdown", "id": "dc23c48e", "metadata": {}, "source": [ "# Google Serper API\n", "\n", "This notebook goes over how to use the Google Serper component to search the web. First you need to sign up for a free account at [serper.dev](https://serper.dev) and get your api key." ] }, { "cell_type": "code", "execution_count": 11, "outputs": [], "source": [ "import os\n", "import pprint\n", "os.environ[\"SERPER_API_KEY\"] = \"\"" ], "metadata": { "collapsed": false, "pycharm": { "is_executing": true }, "ExecuteTime": { "end_time": "2023-05-04T00:56:29.336521Z", "start_time": "2023-05-04T00:56:29.334173Z" } } }, { "cell_type": "code", "execution_count": 2, "id": "54bf5afd", "metadata": { "ExecuteTime": { "end_time": "2023-05-04T00:54:07.676293Z", "start_time": "2023-05-04T00:54:06.665742Z" } }, "outputs": [], "source": [ "from langchain.utilities import GoogleSerperAPIWrapper" ] }, { "cell_type": "code", "execution_count": 3, "id": "31f8f382", "metadata": { "ExecuteTime": { "end_time": "2023-05-04T00:54:08.324245Z", "start_time": "2023-05-04T00:54:08.321577Z" } }, "outputs": [], "source": [ "search = GoogleSerperAPIWrapper()" ] }, { "cell_type": "code", "execution_count": 4, "id": "25ce0225", "metadata": { "ExecuteTime": { "end_time": "2023-05-04T00:54:11.399847Z", "start_time": "2023-05-04T00:54:09.335597Z" } }, "outputs": [ { "data": { "text/plain": "'Barack Hussein Obama II'" }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "search.run(\"Obama's first name?\")" ] }, { "cell_type": "markdown", "source": [ "## As part of a Self Ask With Search Chain" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 5, "outputs": [], "source": [ "os.environ['OPENAI_API_KEY'] = \"\"" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-05-04T00:54:14.311773Z", "start_time": "2023-05-04T00:54:14.304389Z" } } }, { "cell_type": "code", "execution_count": 5, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001B[1m> Entering new AgentExecutor chain...\u001B[0m\n", "\u001B[32;1m\u001B[1;3m Yes.\n", "Follow up: Who is the reigning men's U.S. Open champion?\u001B[0m\n", "Intermediate answer: \u001B[36;1m\u001B[1;3mCurrent champions Carlos Alcaraz, 2022 men's singles champion.\u001B[0m\n", "\u001B[32;1m\u001B[1;3mFollow up: Where is Carlos Alcaraz from?\u001B[0m\n", "Intermediate answer: \u001B[36;1m\u001B[1;3mEl Palmar, Spain\u001B[0m\n", "\u001B[32;1m\u001B[1;3mSo the final answer is: El Palmar, Spain\u001B[0m\n", "\n", "\u001B[1m> Finished chain.\u001B[0m\n" ] }, { "data": { "text/plain": "'El Palmar, Spain'" }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.utilities import GoogleSerperAPIWrapper\n", "from langchain.llms.openai import OpenAI\n", "from langchain.agents import initialize_agent, Tool\n", "from langchain.agents import AgentType\n", "\n", "llm = OpenAI(temperature=0)\n", "search = GoogleSerperAPIWrapper()\n", "tools = [\n", " Tool(\n", " name=\"Intermediate Answer\",\n", " func=search.run,\n", " description=\"useful for when you need to ask with search\"\n", " )\n", "]\n", "\n", "self_ask_with_search = initialize_agent(tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True)\n", "self_ask_with_search.run(\"What is the hometown of the reigning men's U.S. Open champion?\")" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "## Obtaining results with metadata\n", "If you would also like to obtain the results in a structured way including metadata. For this we will be using the `results` method of the wrapper." ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 6, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'searchParameters': {'q': 'Apple Inc.',\n", " 'gl': 'us',\n", " 'hl': 'en',\n", " 'num': 10,\n", " 'type': 'search'},\n", " 'knowledgeGraph': {'title': 'Apple',\n", " 'type': 'Technology company',\n", " 'website': 'http://www.apple.com/',\n", " 'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQwGQRv5TjjkycpctY66mOg_e2-npacrmjAb6_jAWhzlzkFE3OTjxyzbA&s=0',\n", " 'description': 'Apple Inc. is an American multinational '\n", " 'technology company headquartered in '\n", " 'Cupertino, California. Apple is the '\n", " \"world's largest technology company by \"\n", " 'revenue, with US$394.3 billion in 2022 '\n", " 'revenue. As of March 2023, Apple is the '\n", " \"world's biggest...\",\n", " 'descriptionSource': 'Wikipedia',\n", " 'descriptionLink': 'https://en.wikipedia.org/wiki/Apple_Inc.',\n", " 'attributes': {'Customer service': '1 (800) 275-2273',\n", " 'CEO': 'Tim Cook (Aug 24, 2011–)',\n", " 'Headquarters': 'Cupertino, CA',\n", " 'Founded': 'April 1, 1976, Los Altos, CA',\n", " 'Founders': 'Steve Jobs, Steve Wozniak, '\n", " 'Ronald Wayne, and more',\n", " 'Products': 'iPhone, iPad, Apple TV, and '\n", " 'more'}},\n", " 'organic': [{'title': 'Apple',\n", " 'link': 'https://www.apple.com/',\n", " 'snippet': 'Discover the innovative world of Apple and shop '\n", " 'everything iPhone, iPad, Apple Watch, Mac, and Apple '\n", " 'TV, plus explore accessories, entertainment, ...',\n", " 'sitelinks': [{'title': 'Support',\n", " 'link': 'https://support.apple.com/'},\n", " {'title': 'iPhone',\n", " 'link': 'https://www.apple.com/iphone/'},\n", " {'title': 'Site Map',\n", " 'link': 'https://www.apple.com/sitemap/'},\n", " {'title': 'Business',\n", " 'link': 'https://www.apple.com/business/'},\n", " {'title': 'Mac',\n", " 'link': 'https://www.apple.com/mac/'},\n", " {'title': 'Watch',\n", " 'link': 'https://www.apple.com/watch/'}],\n", " 'position': 1},\n", " {'title': 'Apple Inc. - Wikipedia',\n", " 'link': 'https://en.wikipedia.org/wiki/Apple_Inc.',\n", " 'snippet': 'Apple Inc. is an American multinational technology '\n", " 'company headquartered in Cupertino, California. '\n", " \"Apple is the world's largest technology company by \"\n", " 'revenue, ...',\n", " 'attributes': {'Products': 'AirPods; Apple Watch; iPad; iPhone; '\n", " 'Mac; Full list',\n", " 'Founders': 'Steve Jobs; Steve Wozniak; Ronald '\n", " 'Wayne; Mike Markkula'},\n", " 'sitelinks': [{'title': 'History',\n", " 'link': 'https://en.wikipedia.org/wiki/History_of_Apple_Inc.'},\n", " {'title': 'Timeline of Apple Inc. products',\n", " 'link': 'https://en.wikipedia.org/wiki/Timeline_of_Apple_Inc._products'},\n", " {'title': 'Litigation involving Apple Inc.',\n", " 'link': 'https://en.wikipedia.org/wiki/Litigation_involving_Apple_Inc.'},\n", " {'title': 'Apple Store',\n", " 'link': 'https://en.wikipedia.org/wiki/Apple_Store'}],\n", " 'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRvmB5fT1LjqpZx02UM7IJq0Buoqt0DZs_y0dqwxwSWyP4PIN9FaxuTea0&s',\n", " 'position': 2},\n", " {'title': 'Apple Inc. | History, Products, Headquarters, & Facts '\n", " '| Britannica',\n", " 'link': 'https://www.britannica.com/topic/Apple-Inc',\n", " 'snippet': 'Apple Inc., formerly Apple Computer, Inc., American '\n", " 'manufacturer of personal computers, smartphones, '\n", " 'tablet computers, computer peripherals, and computer '\n", " '...',\n", " 'attributes': {'Related People': 'Steve Jobs Steve Wozniak Jony '\n", " 'Ive Tim Cook Angela Ahrendts',\n", " 'Date': '1976 - present'},\n", " 'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS3liELlhrMz3Wpsox29U8jJ3L8qETR0hBWHXbFnwjwQc34zwZvFELst2E&s',\n", " 'position': 3},\n", " {'title': 'AAPL: Apple Inc Stock Price Quote - NASDAQ GS - '\n", " 'Bloomberg.com',\n", " 'link': 'https://www.bloomberg.com/quote/AAPL:US',\n", " 'snippet': 'AAPL:USNASDAQ GS. Apple Inc. COMPANY INFO ; Open. '\n", " '170.09 ; Prev Close. 169.59 ; Volume. 48,425,696 ; '\n", " 'Market Cap. 2.667T ; Day Range. 167.54170.35.',\n", " 'position': 4},\n", " {'title': 'Apple Inc. (AAPL) Company Profile & Facts - Yahoo '\n", " 'Finance',\n", " 'link': 'https://finance.yahoo.com/quote/AAPL/profile/',\n", " 'snippet': 'Apple Inc. designs, manufactures, and markets '\n", " 'smartphones, personal computers, tablets, wearables, '\n", " 'and accessories worldwide. The company offers '\n", " 'iPhone, a line ...',\n", " 'position': 5},\n", " {'title': 'Apple Inc. (AAPL) Stock Price, News, Quote & History - '\n", " 'Yahoo Finance',\n", " 'link': 'https://finance.yahoo.com/quote/AAPL',\n", " 'snippet': 'Find the latest Apple Inc. (AAPL) stock quote, '\n", " 'history, news and other vital information to help '\n", " 'you with your stock trading and investing.',\n", " 'position': 6}],\n", " 'peopleAlsoAsk': [{'question': 'What does Apple Inc do?',\n", " 'snippet': 'Apple Inc. (Apple) designs, manufactures and '\n", " 'markets smartphones, personal\\n'\n", " 'computers, tablets, wearables and accessories '\n", " 'and sells a range of related\\n'\n", " 'services.',\n", " 'title': 'AAPL.O - | Stock Price & Latest News - Reuters',\n", " 'link': 'https://www.reuters.com/markets/companies/AAPL.O/'},\n", " {'question': 'What is the full form of Apple Inc?',\n", " 'snippet': '(formerly Apple Computer Inc.) is an American '\n", " 'computer and consumer electronics\\n'\n", " 'company famous for creating the iPhone, iPad '\n", " 'and Macintosh computers.',\n", " 'title': 'What is Apple? An products and history overview '\n", " '- TechTarget',\n", " 'link': 'https://www.techtarget.com/whatis/definition/Apple'},\n", " {'question': 'What is Apple Inc iPhone?',\n", " 'snippet': 'Apple Inc (Apple) designs, manufactures, and '\n", " 'markets smartphones, tablets,\\n'\n", " 'personal computers, and wearable devices. The '\n", " 'company also offers software\\n'\n", " 'applications and related services, '\n", " 'accessories, and third-party digital content.\\n'\n", " \"Apple's product portfolio includes iPhone, \"\n", " 'iPad, Mac, iPod, Apple Watch, and\\n'\n", " 'Apple TV.',\n", " 'title': 'Apple Inc Company Profile - Apple Inc Overview - '\n", " 'GlobalData',\n", " 'link': 'https://www.globaldata.com/company-profile/apple-inc/'},\n", " {'question': 'Who runs Apple Inc?',\n", " 'snippet': 'Timothy Donald Cook (born November 1, 1960) is '\n", " 'an American business executive\\n'\n", " 'who has been the chief executive officer of '\n", " 'Apple Inc. since 2011. Cook\\n'\n", " \"previously served as the company's chief \"\n", " 'operating officer under its co-founder\\n'\n", " 'Steve Jobs. He is the first CEO of any Fortune '\n", " '500 company who is openly gay.',\n", " 'title': 'Tim Cook - Wikipedia',\n", " 'link': 'https://en.wikipedia.org/wiki/Tim_Cook'}],\n", " 'relatedSearches': [{'query': 'Who invented the iPhone'},\n", " {'query': 'Apple iPhone'},\n", " {'query': 'History of Apple company PDF'},\n", " {'query': 'Apple company history'},\n", " {'query': 'Apple company introduction'},\n", " {'query': 'Apple India'},\n", " {'query': 'What does Apple Inc own'},\n", " {'query': 'Apple Inc After Steve'},\n", " {'query': 'Apple Watch'},\n", " {'query': 'Apple App Store'}]}\n" ] } ], "source": [ "search = GoogleSerperAPIWrapper()\n", "results = search.results(\"Apple Inc.\")\n", "pprint.pp(results)" ], "metadata": { "collapsed": false, "pycharm": { "is_executing": true }, "ExecuteTime": { "end_time": "2023-05-04T00:54:22.863413Z", "start_time": "2023-05-04T00:54:20.827395Z" } } }, { "cell_type": "markdown", "source": [ "## Searching for Google Images\n", "We can also query Google Images using this wrapper. For example:" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 7, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'searchParameters': {'q': 'Lion',\n", " 'gl': 'us',\n", " 'hl': 'en',\n", " 'num': 10,\n", " 'type': 'images'},\n", " 'images': [{'title': 'Lion - Wikipedia',\n", " 'imageUrl': 'https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Lion_waiting_in_Namibia.jpg/1200px-Lion_waiting_in_Namibia.jpg',\n", " 'imageWidth': 1200,\n", " 'imageHeight': 900,\n", " 'thumbnailUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRye79ROKwjfb6017jr0iu8Bz2E1KKuHg-A4qINJaspyxkZrkw&s',\n", " 'thumbnailWidth': 259,\n", " 'thumbnailHeight': 194,\n", " 'source': 'Wikipedia',\n", " 'domain': 'en.wikipedia.org',\n", " 'link': 'https://en.wikipedia.org/wiki/Lion',\n", " 'position': 1},\n", " {'title': 'Lion | Characteristics, Habitat, & Facts | Britannica',\n", " 'imageUrl': 'https://cdn.britannica.com/55/2155-050-604F5A4A/lion.jpg',\n", " 'imageWidth': 754,\n", " 'imageHeight': 752,\n", " 'thumbnailUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS3fnDub1GSojI0hJ-ZGS8Tv-hkNNloXh98DOwXZoZ_nUs3GWSd&s',\n", " 'thumbnailWidth': 225,\n", " 'thumbnailHeight': 224,\n", " 'source': 'Encyclopedia Britannica',\n", " 'domain': 'www.britannica.com',\n", " 'link': 'https://www.britannica.com/animal/lion',\n", " 'position': 2},\n", " {'title': 'African lion, facts and photos',\n", " 'imageUrl': 'https://i.natgeofe.com/n/487a0d69-8202-406f-a6a0-939ed3704693/african-lion.JPG',\n", " 'imageWidth': 3072,\n", " 'imageHeight': 2043,\n", " 'thumbnailUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTPlTarrtDbyTiEm-VI_PML9VtOTVPuDXJ5ybDf_lN11H2mShk&s',\n", " 'thumbnailWidth': 275,\n", " 'thumbnailHeight': 183,\n", " 'source': 'National Geographic',\n", " 'domain': 'www.nationalgeographic.com',\n", " 'link': 'https://www.nationalgeographic.com/animals/mammals/facts/african-lion',\n", " 'position': 3},\n", " {'title': 'Saint Louis Zoo | African Lion',\n", " 'imageUrl': 'https://optimise2.assets-servd.host/maniacal-finch/production/animals/african-lion-01-01.jpg?w=1200&auto=compress%2Cformat&fit=crop&dm=1658933674&s=4b63f926a0f524f2087a8e0613282bdb',\n", " 'imageWidth': 1200,\n", " 'imageHeight': 1200,\n", " 'thumbnailUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTlewcJ5SwC7yKup6ByaOjTnAFDeoOiMxyJTQaph2W_I3dnks4&s',\n", " 'thumbnailWidth': 225,\n", " 'thumbnailHeight': 225,\n", " 'source': 'St. Louis Zoo',\n", " 'domain': 'stlzoo.org',\n", " 'link': 'https://stlzoo.org/animals/mammals/carnivores/lion',\n", " 'position': 4},\n", " {'title': 'How to Draw a Realistic Lion like an Artist - Studio '\n", " 'Wildlife',\n", " 'imageUrl': 'https://studiowildlife.com/wp-content/uploads/2021/10/245528858_183911853822648_6669060845725210519_n.jpg',\n", " 'imageWidth': 1431,\n", " 'imageHeight': 2048,\n", " 'thumbnailUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTmn5HayVj3wqoBDQacnUtzaDPZzYHSLKUlIEcni6VB8w0mVeA&s',\n", " 'thumbnailWidth': 188,\n", " 'thumbnailHeight': 269,\n", " 'source': 'Studio Wildlife',\n", " 'domain': 'studiowildlife.com',\n", " 'link': 'https://studiowildlife.com/how-to-draw-a-realistic-lion-like-an-artist/',\n", " 'position': 5},\n", " {'title': 'Lion | Characteristics, Habitat, & Facts | Britannica',\n", " 'imageUrl': 'https://cdn.britannica.com/29/150929-050-547070A1/lion-Kenya-Masai-Mara-National-Reserve.jpg',\n", " 'imageWidth': 1600,\n", " 'imageHeight': 1085,\n", " 'thumbnailUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSCqaKY_THr0IBZN8c-2VApnnbuvKmnsWjfrwKoWHFR9w3eN5o&s',\n", " 'thumbnailWidth': 273,\n", " 'thumbnailHeight': 185,\n", " 'source': 'Encyclopedia Britannica',\n", " 'domain': 'www.britannica.com',\n", " 'link': 'https://www.britannica.com/animal/lion',\n", " 'position': 6},\n", " {'title': \"Where do lions live? Facts about lions' habitats and \"\n", " 'other cool facts',\n", " 'imageUrl': 'https://www.gannett-cdn.com/-mm-/b2b05a4ab25f4fca0316459e1c7404c537a89702/c=0-0-1365-768/local/-/media/2022/03/16/USATODAY/usatsports/imageForEntry5-ODq.jpg?width=1365&height=768&fit=crop&format=pjpg&auto=webp',\n", " 'imageWidth': 1365,\n", " 'imageHeight': 768,\n", " 'thumbnailUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTc_4vCHscgvFvYy3PSrtIOE81kNLAfhDK8F3mfOuotL0kUkbs&s',\n", " 'thumbnailWidth': 299,\n", " 'thumbnailHeight': 168,\n", " 'source': 'USA Today',\n", " 'domain': 'www.usatoday.com',\n", " 'link': 'https://www.usatoday.com/story/news/2023/01/08/where-do-lions-live-habitat/10927718002/',\n", " 'position': 7},\n", " {'title': 'Lion',\n", " 'imageUrl': 'https://i.natgeofe.com/k/1d33938b-3d02-4773-91e3-70b113c3b8c7/lion-male-roar_square.jpg',\n", " 'imageWidth': 3072,\n", " 'imageHeight': 3072,\n", " 'thumbnailUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQqLfnBrBLcTiyTZynHH3FGbBtX2bd1ScwpcuOLnksTyS9-4GM&s',\n", " 'thumbnailWidth': 225,\n", " 'thumbnailHeight': 225,\n", " 'source': 'National Geographic Kids',\n", " 'domain': 'kids.nationalgeographic.com',\n", " 'link': 'https://kids.nationalgeographic.com/animals/mammals/facts/lion',\n", " 'position': 8},\n", " {'title': \"Lion | Smithsonian's National Zoo\",\n", " 'imageUrl': 'https://nationalzoo.si.edu/sites/default/files/styles/1400_scale/public/animals/exhibit/africanlion-005.jpg?itok=6wA745g_',\n", " 'imageWidth': 1400,\n", " 'imageHeight': 845,\n", " 'thumbnailUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSgB3z_D4dMEOWJ7lajJk4XaQSL4DdUvIRj4UXZ0YoE5fGuWuo&s',\n", " 'thumbnailWidth': 289,\n", " 'thumbnailHeight': 174,\n", " 'source': \"Smithsonian's National Zoo\",\n", " 'domain': 'nationalzoo.si.edu',\n", " 'link': 'https://nationalzoo.si.edu/animals/lion',\n", " 'position': 9},\n", " {'title': \"Zoo's New Male Lion Explores Habitat for the First Time \"\n", " '- Virginia Zoo',\n", " 'imageUrl': 'https://virginiazoo.org/wp-content/uploads/2022/04/ZOO_0056-scaled.jpg',\n", " 'imageWidth': 2560,\n", " 'imageHeight': 2141,\n", " 'thumbnailUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTDCG7XvXRCwpe_-Vy5mpvrQpVl5q2qwgnDklQhrJpQzObQGz4&s',\n", " 'thumbnailWidth': 246,\n", " 'thumbnailHeight': 205,\n", " 'source': 'Virginia Zoo',\n", " 'domain': 'virginiazoo.org',\n", " 'link': 'https://virginiazoo.org/zoos-new-male-lion-explores-habitat-for-thefirst-time/',\n", " 'position': 10}]}\n" ] } ], "source": [ "search = GoogleSerperAPIWrapper(type=\"images\")\n", "results = search.results(\"Lion\")\n", "pprint.pp(results)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-05-04T00:54:27.879867Z", "start_time": "2023-05-04T00:54:26.380022Z" } } }, { "cell_type": "markdown", "source": [ "## Searching for Google News\n", "We can also query Google News using this wrapper. For example:" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 8, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'searchParameters': {'q': 'Tesla Inc.',\n", " 'gl': 'us',\n", " 'hl': 'en',\n", " 'num': 10,\n", " 'type': 'news'},\n", " 'news': [{'title': 'ISS recommends Tesla investors vote against re-election '\n", " 'of Robyn Denholm',\n", " 'link': 'https://www.reuters.com/business/autos-transportation/iss-recommends-tesla-investors-vote-against-re-election-robyn-denholm-2023-05-04/',\n", " 'snippet': 'Proxy advisory firm ISS on Wednesday recommended Tesla '\n", " 'investors vote against re-election of board chair Robyn '\n", " 'Denholm, citing \"concerns on...',\n", " 'date': '5 mins ago',\n", " 'source': 'Reuters',\n", " 'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcROdETe_GUyp1e8RHNhaRM8Z_vfxCvdfinZwzL1bT1ZGSYaGTeOojIdBoLevA&s',\n", " 'position': 1},\n", " {'title': 'Global companies by market cap: Tesla fell most in April',\n", " 'link': 'https://www.reuters.com/markets/global-companies-by-market-cap-tesla-fell-most-april-2023-05-02/',\n", " 'snippet': 'Tesla Inc was the biggest loser among top companies by '\n", " 'market capitalisation in April, hit by disappointing '\n", " 'quarterly earnings after it...',\n", " 'date': '1 day ago',\n", " 'source': 'Reuters',\n", " 'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ4u4CP8aOdGyRFH6o4PkXi-_eZDeY96vLSag5gDjhKMYf98YBER2cZPbkStQ&s',\n", " 'position': 2},\n", " {'title': 'Tesla Wanted an EV Price War. Ford Showed Up.',\n", " 'link': 'https://www.bloomberg.com/opinion/articles/2023-05-03/tesla-wanted-an-ev-price-war-ford-showed-up',\n", " 'snippet': 'The legacy automaker is paring back the cost of its '\n", " 'Mustang Mach-E model after Tesla discounted its '\n", " 'competing EVs, portending tighter...',\n", " 'date': '6 hours ago',\n", " 'source': 'Bloomberg.com',\n", " 'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS_3Eo4VI0H-nTeIbYc5DaQn5ep7YrWnmhx6pv8XddFgNF5zRC9gEpHfDq8yQ&s',\n", " 'position': 3},\n", " {'title': 'Joby Aviation to get investment from Tesla shareholder '\n", " 'Baillie Gifford',\n", " 'link': 'https://finance.yahoo.com/news/joby-aviation-investment-tesla-shareholder-204450712.html',\n", " 'snippet': 'This comes days after Joby clinched a $55 million '\n", " 'contract extension to deliver up to nine air taxis to '\n", " 'the U.S. Air Force,...',\n", " 'date': '4 hours ago',\n", " 'source': 'Yahoo Finance',\n", " 'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQO0uVn297LI-xryrPNqJ-apUOulj4ohM-xkN4OfmvMOYh1CPdUEBbYx6hviw&s',\n", " 'position': 4},\n", " {'title': 'Tesla resumes U.S. orders for a Model 3 version at lower '\n", " 'price, range',\n", " 'link': 'https://finance.yahoo.com/news/tesla-resumes-us-orders-model-045736115.html',\n", " 'snippet': '(Reuters) -Tesla Inc has resumed taking orders for its '\n", " 'Model 3 long-range vehicle in the United States, the '\n", " \"company's website showed late on...\",\n", " 'date': '19 hours ago',\n", " 'source': 'Yahoo Finance',\n", " 'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTIZetJ62sQefPfbQ9KKDt6iH7Mc0ylT5t_hpgeeuUkHhJuAx2FOJ4ZTRVDFg&s',\n", " 'position': 5},\n", " {'title': 'The Tesla Model 3 Long Range AWD Is Now Available in the '\n", " 'U.S. With 325 Miles of Range',\n", " 'link': 'https://www.notateslaapp.com/news/1393/tesla-reopens-orders-for-model-3-long-range-after-months-of-unavailability',\n", " 'snippet': 'Tesla has reopened orders for the Model 3 Long Range '\n", " 'RWD, which has been unavailable for months due to high '\n", " 'demand.',\n", " 'date': '7 hours ago',\n", " 'source': 'Not a Tesla App',\n", " 'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSecrgxZpRj18xIJY-nDHljyP-A4ejEkswa9eq77qhMNrScnVIqe34uql5U4w&s',\n", " 'position': 6},\n", " {'title': 'Tesla Cybertruck alpha prototype spotted at the Fremont '\n", " 'factory in new pics and videos',\n", " 'link': 'https://www.teslaoracle.com/2023/05/03/tesla-cybertruck-alpha-prototype-interior-and-exterior-spotted-at-the-fremont-factory-in-new-pics-and-videos/',\n", " 'snippet': 'A Tesla Cybertruck alpha prototype goes to Fremont, '\n", " 'California for another round of testing before going to '\n", " 'production later this year (pics...',\n", " 'date': '14 hours ago',\n", " 'source': 'Tesla Oracle',\n", " 'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRO7M5ZLQE-Zo4-_5dv9hNAQZ3wSqfvYCuKqzxHG-M6CgLpwPMMG_ssebdcMg&s',\n", " 'position': 7},\n", " {'title': 'Tesla putting facility in new part of country - Austin '\n", " 'Business Journal',\n", " 'link': 'https://www.bizjournals.com/austin/news/2023/05/02/tesla-leases-building-seattle-area.html',\n", " 'snippet': 'Check out what Puget Sound Business Journal has to '\n", " \"report about the Austin-based company's real estate \"\n", " 'footprint in the Pacific Northwest.',\n", " 'date': '22 hours ago',\n", " 'source': 'The Business Journals',\n", " 'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR9kIEHWz1FcHKDUtGQBS0AjmkqtyuBkQvD8kyIY3kpaPrgYaN7I_H2zoOJsA&s',\n", " 'position': 8},\n", " {'title': 'Tesla (TSLA) Resumes Orders for Model 3 Long Range After '\n", " 'Backlog',\n", " 'link': 'https://www.bloomberg.com/news/articles/2023-05-03/tesla-resumes-orders-for-popular-model-3-long-range-at-47-240',\n", " 'snippet': 'Tesla Inc. has resumed taking orders for its Model 3 '\n", " 'Long Range edition with a starting price of $47240, '\n", " 'according to its website.',\n", " 'date': '5 hours ago',\n", " 'source': 'Bloomberg.com',\n", " 'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTWWIC4VpMTfRvSyqiomODOoLg0xhoBf-Tc1qweKnSuaiTk-Y1wMJZM3jct0w&s',\n", " 'position': 9}]}\n" ] } ], "source": [ "search = GoogleSerperAPIWrapper(type=\"news\")\n", "results = search.results(\"Tesla Inc.\")\n", "pprint.pp(results)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-05-04T00:54:34.984087Z", "start_time": "2023-05-04T00:54:33.369231Z" } } }, { "cell_type": "markdown", "source": [ "If you want to only receive news articles published in the last hour, you can do the following:" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 9, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'searchParameters': {'q': 'Tesla Inc.',\n", " 'gl': 'us',\n", " 'hl': 'en',\n", " 'num': 10,\n", " 'type': 'news',\n", " 'tbs': 'qdr:h'},\n", " 'news': [{'title': 'Oklahoma Gov. Stitt sees growing foreign interest in '\n", " 'investments in ...',\n", " 'link': 'https://www.reuters.com/world/us/oklahoma-gov-stitt-sees-growing-foreign-interest-investments-state-2023-05-04/',\n", " 'snippet': 'T)), a battery supplier to electric vehicle maker Tesla '\n", " 'Inc (TSLA.O), said on Sunday it is considering building '\n", " 'a battery plant in Oklahoma, its third in...',\n", " 'date': '53 mins ago',\n", " 'source': 'Reuters',\n", " 'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSSTcsXeenqmEKdiekvUgAmqIPR4nlAmgjTkBqLpza-lLfjX1CwB84MoNVj0Q&s',\n", " 'position': 1},\n", " {'title': 'Ryder lanza solución llave en mano para vehículos '\n", " 'eléctricos en EU',\n", " 'link': 'https://www.tyt.com.mx/nota/ryder-lanza-solucion-llave-en-mano-para-vehiculos-electricos-en-eu',\n", " 'snippet': 'Ryder System Inc. presentó RyderElectric+ TM como su '\n", " 'nueva solución llave en mano ... Ryder también tiene '\n", " 'reservados los semirremolques Tesla y continúa...',\n", " 'date': '56 mins ago',\n", " 'source': 'Revista Transportes y Turismo',\n", " 'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQJhXTQQtjSUZf9YPM235WQhFU5_d7lEA76zB8DGwZfixcgf1_dhPJyKA1Nbw&s',\n", " 'position': 2},\n", " {'title': '\"I think people can get by with $999 million,\" Bernie '\n", " 'Sanders tells American Billionaires.',\n", " 'link': 'https://thebharatexpressnews.com/i-think-people-can-get-by-with-999-million-bernie-sanders-tells-american-billionaires-heres-how-the-ultra-rich-can-pay-less-income-tax-than-you-legally/',\n", " 'snippet': 'The report noted that in 2007 and 2011, Amazon.com Inc. '\n", " 'founder Jeff Bezos “did not pay a dime in federal ... '\n", " 'If you want to bet on Musk, check out Tesla.',\n", " 'date': '11 mins ago',\n", " 'source': 'THE BHARAT EXPRESS NEWS',\n", " 'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR_X9qqSwVFBBdos2CK5ky5IWIE3aJPCQeRYR9O1Jz4t-MjaEYBuwK7AU3AJQ&s',\n", " 'position': 3}]}\n" ] } ], "source": [ "search = GoogleSerperAPIWrapper(type=\"news\", tbs=\"qdr:h\")\n", "results = search.results(\"Tesla Inc.\")\n", "pprint.pp(results)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-05-04T00:54:41.786864Z", "start_time": "2023-05-04T00:54:40.691905Z" } } }, { "cell_type": "markdown", "source": [ "Some examples of the `tbs` parameter:\n", "\n", "`qdr:h` (past hour)\n", "`qdr:d` (past day)\n", "`qdr:w` (past week)\n", "`qdr:m` (past month)\n", "`qdr:y` (past year)\n", "\n", "You can specify intermediate time periods by adding a number:\n", "`qdr:h12` (past 12 hours)\n", "`qdr:d3` (past 3 days)\n", "`qdr:w2` (past 2 weeks)\n", "`qdr:m6` (past 6 months)\n", "`qdr:m2` (past 2 years)\n", "\n", "For all supported filters simply go to [Google Search](https://google.com), search for something, click on \"Tools\", add your date filter and check the URL for \"tbs=\".\n" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "## Searching for Google Places\n", "We can also query Google Places using this wrapper. For example:" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 10, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'searchParameters': {'q': 'Italian restaurants in Upper East Side',\n", " 'gl': 'us',\n", " 'hl': 'en',\n", " 'num': 10,\n", " 'type': 'places'},\n", " 'places': [{'position': 1,\n", " 'title': \"L'Osteria\",\n", " 'address': '1219 Lexington Ave',\n", " 'latitude': 40.777154599999996,\n", " 'longitude': -73.9571363,\n", " 'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipNjU7BWEq_aYQANBCbX52Kb0lDpd_lFIx5onw40=w92-h92-n-k-no',\n", " 'rating': 4.7,\n", " 'ratingCount': 91,\n", " 'category': 'Italian'},\n", " {'position': 2,\n", " 'title': \"Tony's Di Napoli\",\n", " 'address': '1081 3rd Ave',\n", " 'latitude': 40.7643567,\n", " 'longitude': -73.9642373,\n", " 'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipNbNv6jZkJ9nyVi60__8c1DQbe_eEbugRAhIYye=w92-h92-n-k-no',\n", " 'rating': 4.5,\n", " 'ratingCount': 2265,\n", " 'category': 'Italian'},\n", " {'position': 3,\n", " 'title': 'Caravaggio',\n", " 'address': '23 E 74th St',\n", " 'latitude': 40.773412799999996,\n", " 'longitude': -73.96473379999999,\n", " 'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipPDGchokDvppoLfmVEo6X_bWd3Fz0HyxIHTEe9V=w92-h92-n-k-no',\n", " 'rating': 4.5,\n", " 'ratingCount': 276,\n", " 'category': 'Italian'},\n", " {'position': 4,\n", " 'title': 'Luna Rossa',\n", " 'address': '347 E 85th St',\n", " 'latitude': 40.776593999999996,\n", " 'longitude': -73.950351,\n", " 'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipNPCpCPuqPAb1Mv6_fOP7cjb8Wu1rbqbk2sMBlh=w92-h92-n-k-no',\n", " 'rating': 4.5,\n", " 'ratingCount': 140,\n", " 'category': 'Italian'},\n", " {'position': 5,\n", " 'title': \"Paola's\",\n", " 'address': '1361 Lexington Ave',\n", " 'latitude': 40.7822019,\n", " 'longitude': -73.9534096,\n", " 'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipPJr2Vcx-B6K-GNQa4koOTffggTePz8TKRTnWi3=w92-h92-n-k-no',\n", " 'rating': 4.5,\n", " 'ratingCount': 344,\n", " 'category': 'Italian'},\n", " {'position': 6,\n", " 'title': 'Come Prima',\n", " 'address': '903 Madison Ave',\n", " 'latitude': 40.772124999999996,\n", " 'longitude': -73.965012,\n", " 'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipNrX19G0NVdtDyMovCQ-M-m0c_gLmIxrWDQAAbz=w92-h92-n-k-no',\n", " 'rating': 4.5,\n", " 'ratingCount': 176,\n", " 'category': 'Italian'},\n", " {'position': 7,\n", " 'title': 'Botte UES',\n", " 'address': '1606 1st Ave.',\n", " 'latitude': 40.7750785,\n", " 'longitude': -73.9504801,\n", " 'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipPPN5GXxfH3NDacBc0Pt3uGAInd9OChS5isz9RF=w92-h92-n-k-no',\n", " 'rating': 4.4,\n", " 'ratingCount': 152,\n", " 'category': 'Italian'},\n", " {'position': 8,\n", " 'title': 'Piccola Cucina Uptown',\n", " 'address': '106 E 60th St',\n", " 'latitude': 40.7632468,\n", " 'longitude': -73.9689825,\n", " 'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipPifIgzOCD5SjgzzqBzGkdZCBp0MQsK5k7M7znn=w92-h92-n-k-no',\n", " 'rating': 4.6,\n", " 'ratingCount': 941,\n", " 'category': 'Italian'},\n", " {'position': 9,\n", " 'title': 'Pinocchio Restaurant',\n", " 'address': '300 E 92nd St',\n", " 'latitude': 40.781453299999995,\n", " 'longitude': -73.9486788,\n", " 'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipNtxlIyEEJHtDtFtTR9nB38S8A2VyMu-mVVz72A=w92-h92-n-k-no',\n", " 'rating': 4.5,\n", " 'ratingCount': 113,\n", " 'category': 'Italian'},\n", " {'position': 10,\n", " 'title': 'Barbaresco',\n", " 'address': '843 Lexington Ave #1',\n", " 'latitude': 40.7654332,\n", " 'longitude': -73.9656873,\n", " 'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipMb9FbPuXF_r9g5QseOHmReejxSHgSahPMPJ9-8=w92-h92-n-k-no',\n", " 'rating': 4.3,\n", " 'ratingCount': 122,\n", " 'locationHint': 'In The Touraine',\n", " 'category': 'Italian'}]}\n" ] } ], "source": [ "search = GoogleSerperAPIWrapper(type=\"places\")\n", "results = search.results(\"Italian restaurants in Upper East Side\")\n", "pprint.pp(results)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-05-04T00:56:07.271164Z", "start_time": "2023-05-04T00:56:05.645847Z" } } } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }