|
| 1 | +.. serpapi-python documentation master file, created by |
| 2 | + sphinx-quickstart on Sun Apr 3 21:09:40 2022. |
| 3 | + You can adapt this file completely to your liking, but it should at least |
| 4 | + contain the root `toctree` directive. |
| 5 | +
|
| 6 | +**serpapi-python** |
| 7 | +================== |
| 8 | + |
| 9 | +an official Python client library for `SerpApi <https://serpapi.com>`_. |
| 10 | + |
| 11 | +-------------- |
| 12 | + |
| 13 | +Installation |
| 14 | +------------ |
| 15 | + |
| 16 | +To install ``serpapi-python``, simply use `pip`:: |
| 17 | + |
| 18 | + $ pip install serpapi |
| 19 | + |
| 20 | + |
| 21 | +Please note that Python 3.6+ is required. |
| 22 | + |
| 23 | + |
| 24 | +Usage |
| 25 | +----- |
| 26 | + |
| 27 | +Usage of this module is fairly straight-forward. In general, this module attempts to be as close to the actual API as possible, while still being Pythonic. |
| 28 | + |
| 29 | +For example, the API endpoint ``https://serpapi.com/search.json`` is represented by the method ``serpapi.search()``. |
| 30 | + |
| 31 | +.. code-block:: python |
| 32 | +
|
| 33 | + >>> import serpapi |
| 34 | + >>> s = serpapi.search(q="Coffee", engine="google", location="Austin, Texas", hl="en", gl="us") |
| 35 | + >>> s["organic_results"][0]["link"] |
| 36 | + 'https://en.wikipedia.org/wiki/Coffee' |
| 37 | +
|
| 38 | +Any parameters that you pass to ``search()`` will be passed to the API. This includes the ``api_key`` parameter, which is required for all requests. |
| 39 | + |
| 40 | +.. _using-api-client-directly: |
| 41 | + |
| 42 | +Using the API Client directly |
| 43 | +^^^^^^^^^ |
| 44 | + |
| 45 | +To make this less repetitive, and gain the benefit of connection pooling, let's start using the API Client directly:: |
| 46 | + |
| 47 | + >>> client = serpapi.Client(api_key="secret_api_key") |
| 48 | + >>> s = client.search(q="Coffee", engine="google", location="Austin, Texas", hl="en", gl="us") |
| 49 | + |
| 50 | +The ``api_key`` parameter is now automatically passed to all requests made by the client. |
| 51 | + |
| 52 | + |
| 53 | +Concise Tutorial |
| 54 | +---------------- |
| 55 | + |
| 56 | +Let's start by searching for ``Coffee`` on Google:: |
| 57 | + |
| 58 | + >>> import serpapi |
| 59 | + >>> s = serpapi.search(q="Coffee", engine="google", location="Austin, Texas", hl="en", gl="us") |
| 60 | + |
| 61 | +The ``s`` variable now contains a :class:`SerpResults <serpapi.SerpResults>` object, which acts just like a standard dictionary, with some convenient functions added on top. |
| 62 | + |
| 63 | +Let's print the first result:: |
| 64 | + |
| 65 | + >>> print(s["organic_results"][0]["link"]) |
| 66 | + https://en.wikipedia.org/wiki/Coffee |
| 67 | + |
| 68 | +Let's print the title of the first result, but in a more Pythonic way:: |
| 69 | + |
| 70 | + >>> print(s["organic_results"][0].get("title")) |
| 71 | + Coffee - Wikipedia |
| 72 | + |
| 73 | +The `SerpApi.com API Documentation <https://serpapi.com/search-api>`_ contains a list of all the possible parameters that can be passed to the API. |
| 74 | + |
| 75 | + |
| 76 | +API Reference |
| 77 | +------------- |
| 78 | + |
| 79 | +.. _api-reference: |
| 80 | + |
| 81 | +This part of the documentation covers all the interfaces of :class:`serpapi` Python module. |
| 82 | + |
| 83 | +.. module:: serpapi |
| 84 | + :platform: Unix, Windows |
| 85 | + :synopsis: SerpApi Python Library |
| 86 | + |
| 87 | +.. autofunction:: serpapi.search |
| 88 | +.. autofunction:: serpapi.search_archive |
| 89 | +.. autofunction:: serpapi.upload_image |
| 90 | +.. autofunction:: serpapi.locations |
| 91 | +.. autofunction:: serpapi.account |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +Results from SerpApi.com |
| 96 | +------------------------ |
| 97 | + |
| 98 | +When a successful search has been executed, the method returns |
| 99 | +a :class:`SerpResults <serpapi.SerpResults>` object, which acts just like a standard dictionary, |
| 100 | +with some convenient functions added on top. |
| 101 | + |
| 102 | + |
| 103 | +.. code-block:: python |
| 104 | +
|
| 105 | + >>> s = serpapi.search(q="Coffee", engine="google", location="Austin, Texas", hl="en", gl="us") |
| 106 | + >>> type(s) |
| 107 | + <class 'serpapi.models.SerpResults'> |
| 108 | +
|
| 109 | + >>> s["organic_results"][0]["link"] |
| 110 | + 'https://en.wikipedia.org/wiki/Coffee' |
| 111 | +
|
| 112 | + >>> s["search_metadata"] |
| 113 | + {'id': '64c148d35119a60ab1e00cc9', 'status': 'Success', 'json_endpoint': 'https://serpapi.com/searches/a15e1b92727f292c/64c148d35119a60ab1e00cc9.json', 'created_at': '2023-07-26 16:24:51 UTC', 'processed_at': '2023-07-26 16:24:51 UTC', 'google_url': 'https://www.google.com/search?q=Coffee&oq=Coffee&uule=w+CAIQICIdQXVzdGluLFRYLFRleGFzLFVuaXRlZCBTdGF0ZXM&hl=en&gl=us&sourceid=chrome&ie=UTF-8', 'raw_html_file': 'https://serpapi.com/searches/a15e1b92727f292c/64c148d35119a60ab1e00cc9.html', 'total_time_taken': 1.55} |
| 114 | +
|
| 115 | +Optionally, if you want exactly a dictionary of the entire response, you can use the ``as_dict()`` method:: |
| 116 | + |
| 117 | + >>> type(s.as_dict()) |
| 118 | + <class 'dict'> |
| 119 | + |
| 120 | +You can get the next page of results:: |
| 121 | + |
| 122 | + >>> type(s.next_page()) |
| 123 | + <class 'serpapi.models.SerpResults'> |
| 124 | + |
| 125 | +To iterate over all pages of results, it's recommended to :ref:`use the API Client directly <using-api-client-directly>`:: |
| 126 | + |
| 127 | + >>> client = serpapi.Client(api_key="secret_api_key") |
| 128 | + >>> search = client.search(q="Coffee", engine="google", location="Austin, Texas", hl="en", gl="us") |
| 129 | + >>> for page in search.yield_pages(): |
| 130 | + ... print(page["search_metadata"]["page_number"]) |
| 131 | + 1 |
| 132 | + 2 |
| 133 | + 3 |
| 134 | + 4 |
| 135 | + 5 |
| 136 | + 6 |
| 137 | + 7 |
| 138 | + 8 |
| 139 | + 9 |
| 140 | + 10 |
| 141 | + |
| 142 | + |
| 143 | +Here's documentation of the class itself and its methods: |
| 144 | + |
| 145 | +.. autoclass:: serpapi.SerpResults |
| 146 | + |
| 147 | + .. automethod:: SerpResults.next_page |
| 148 | + .. automethod:: SerpResults.yield_pages |
| 149 | + .. autoproperty:: SerpResults.next_page_url |
| 150 | + |
| 151 | + |
| 152 | +API Client |
| 153 | +---------- |
| 154 | + |
| 155 | +The primary interface to `serpapi-python` is through the :class:`serpapi.Client` class. |
| 156 | +The primary benefit of using this class is to benefit from Requests' HTTP Connection Pooling. |
| 157 | +This class also alleviates the need to pass an ``api_key``` along with every search made to the platform. |
| 158 | + |
| 159 | +.. autoclass:: serpapi.Client |
| 160 | + |
| 161 | + .. automethod:: Client.search |
| 162 | + .. automethod:: Client.search_archive |
| 163 | + .. automethod:: Client.upload_image |
| 164 | + .. automethod:: Client.account |
| 165 | + .. automethod:: Client.locations |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | +Exceptions |
| 170 | +---------- |
| 171 | + |
| 172 | +.. autoexception:: serpapi.SerpApiError |
| 173 | + :members: |
| 174 | + |
| 175 | +.. autoexception:: serpapi.SearchIDNotProvided |
| 176 | + :members: |
| 177 | + |
| 178 | +.. autoexception:: serpapi.HTTPError |
| 179 | + :members: |
| 180 | + |
| 181 | +.. autoexception:: serpapi.HTTPConnectionError |
| 182 | + :members: |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | +Indices and tables |
| 189 | +================== |
| 190 | + |
| 191 | +* :ref:`genindex` |
| 192 | +* :ref:`modindex` |
| 193 | +* :ref:`search` |
0 commit comments