Skip to content

Commit 1d03491

Browse files
committed
feat: add support for markdown output (output=md param)
1 parent 561d334 commit 1d03491

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ Documentation is [available on Read the Docs](https://serpapi-python.readthedocs
7979

8080
Change history is [available on GitHub](https://github.com/serpapi/serpapi-python/blob/master/HISTORY.md).
8181

82+
### HTML and Markdown output formats
83+
84+
Searches return JSON as a `SerpResults` object by default. To receive HTML, set `output` to `html`. To receive Markdown, set `output` to `md`:
85+
86+
```python
87+
html = client.search({
88+
"engine": "google",
89+
"q": "coffee",
90+
"output": "html",
91+
})
92+
93+
markdown = client.search({
94+
"engine": "google",
95+
"q": "coffee",
96+
"output": "md",
97+
})
98+
```
99+
100+
HTML and Markdown responses are returned as plain Python strings.
101+
82102
## Basic Examples in Python
83103

84104
### Search Bing

serpapi/core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __repr__(self):
3232
return "<SerpApi Client>"
3333

3434
def search(self, params: dict = None, **kwargs):
35-
"""Fetch a page of results from SerpApi. Returns a :class:`SerpResults <serpapi.client.SerpResults>` object, or unicode text (*e.g.* if ``'output': 'html'`` was passed).
35+
"""Fetch a page of results from SerpApi. Returns a :class:`SerpResults <serpapi.client.SerpResults>` object for JSON responses, or unicode text for HTML and Markdown responses.
3636
3737
The following three calls are equivalent:
3838
@@ -53,7 +53,7 @@ def search(self, params: dict = None, **kwargs):
5353
5454
:param q: typically, this is the parameter for the search engine query.
5555
:param engine: the search engine to use. Defaults to ``google``.
56-
:param output: the output format desired (``html`` or ``json``). Defaults to ``json``.
56+
:param output: the output format desired (``html``, ``json``, or ``md``). Defaults to ``json``.
5757
:param api_key: the API Key to use for SerpApi.com.
5858
:param **: any additional parameters to pass to the API.
5959
@@ -81,7 +81,7 @@ def search_archive(self, params: dict = None, **kwargs):
8181
8282
:param search_id: the Search ID of the search to retrieve from the archive.
8383
:param api_key: the API Key to use for SerpApi.com.
84-
:param output: the output format desired (``html`` or ``json``). Defaults to ``json``.
84+
:param output: the output format desired (``html``, ``json``, or ``md``). Defaults to ``json``.
8585
:param **: any additional parameters to pass to the API.
8686
8787
**Learn more**: https://serpapi.com/search-archive-api

serpapi/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ def from_http_response(cls, r, *, client=None):
9292
Otherwise, the raw text (as a properly decoded unicode string) is returned.
9393
"""
9494

95+
content_type = r.headers.get("Content-Type", "").split(";", 1)[0].lower()
96+
if content_type in {"text/html", "text/markdown"}:
97+
return r.text
98+
9599
try:
96100
cls = cls(r.json(), client=client)
97101

0 commit comments

Comments
 (0)