Spaces:
Running
Running
File size: 44,651 Bytes
cfd3735 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 |
{
"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
}
|