Skip to content

Commit 4b141d6

Browse files
committed
documentation tweak and syntax clean-up
1 parent 488326f commit 4b141d6

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

serpapi/google_scholar_search_results.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class GoogleScholarSearchResults(SerpApiClient):
88
data = query.get_json()
99
```
1010
11-
doc: https://serpapi.com/baidu-search-api
11+
doc: https://serpapi.com/google-scholar-api
1212
"""
1313

1414
def __init__(self, params_dict):

serpapi/serp_api_client.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@
1010
YAHOO_ENGINE = 'yahoo'
1111

1212
class SerpApiClient(object):
13-
"""GoogleSearchResults enables to search google and parse the result.
13+
"""SerpApiClient enables to search any engine support by SerpApi and parse the result.
1414
```python
1515
from serpapi.google_search_results import GoogleSearchResults
16-
query = SerpApiClient({"q": "coffee", "location": "Austin,Texas"}, "google")
17-
data = query.get_json()
16+
client = SerpApiClient({
17+
"q": "Coffee",
18+
"location": "Austin,Texas",
19+
"engine": "google",
20+
"api_key": "<your private key>"
21+
})
22+
data = client.get_json()
1823
```
1924
20-
https://github.com/serpapi/google-search-results-python
25+
https://serpapi.com/search-api
2126
"""
2227

2328
BACKEND = "https://serpapi.com"
@@ -58,7 +63,7 @@ def get_html(self):
5863

5964
def get_json(self):
6065
"""Returns:
61-
Formatted JSON search result
66+
Formatted search result as JSON
6267
"""
6368
self.params_dict["output"] = "json"
6469
return json.loads(self.get_results())
@@ -72,6 +77,7 @@ def get_dictionary(self):
7277
def get_dict(self):
7378
"""Returns:
7479
Dict with the formatted response content
80+
(alias for get_dictionary)
7581
"""
7682
return self.get_dictionary()
7783

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import random
2+
import unittest
3+
import os
4+
import pprint
5+
from serpapi.google_search_results import GoogleSearchResults
6+
7+
class TestSearchApi(unittest.TestCase):
8+
9+
def setUp(self):
10+
GoogleSearchResults.SERP_API_KEY = os.getenv("API_KEY", "demo")
11+
12+
@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided")
13+
def test_get_json(self):
14+
client = GoogleSearchResults({"q": "Coffee", "location": "Austin,Texas"})
15+
data = client.get_json()
16+
self.assertEqual(data["search_metadata"]["status"], "Success")
17+
self.assertIsNotNone(data["search_metadata"]["google_url"])
18+
self.assertIsNotNone(data["search_metadata"]["id"])
19+
# pp = pprint.PrettyPrinter(indent=2)
20+
# pp.pprint(data['local_results'])
21+
self.assertIsNotNone(data['local_results']['places'][0])
22+
23+
@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided")
24+
def test_get_json(self):
25+
client = GoogleSearchResults({"q": "Coffee", "engine": "google_scholar"})
26+
data = client.get_json()
27+
self.assertIsNotNone(data["organic_results"][0]["title"])
28+
29+
@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided")
30+
def test_get_dict(self):
31+
client = GoogleSearchResults({"q": "Coffee", "location": "Austin,Texas"})
32+
data = client.get_dict()
33+
self.assertIsNotNone(data.get('local_results'))
34+
35+
@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided")
36+
def test_get_dictionary(self):
37+
client = GoogleSearchResults({"q": "Coffee", "location": "Austin,Texas"})
38+
data = client.get_dictionary()
39+
self.assertIsNotNone(data.get('local_results'))
40+
41+
@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided")
42+
def test_get_html(self):
43+
client = GoogleSearchResults({"q": "Coffee", "location": "Austin,Texas"})
44+
data = client.get_html()
45+
self.assertGreater(len(data), 10)
46+
47+
if __name__ == '__main__':
48+
unittest.main()

tests/test_serp_api_client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
#
1010
class TestSerpSearchApi(unittest.TestCase):
1111

12-
def setUp(self):
13-
SerpApiClient.SERP_API_KEY = os.getenv("API_KEY", "demo")
14-
1512
@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided")
1613
def test_get_json(self):
17-
client = SerpApiClient({"q": "Coffee", "location": "Austin,Texas", "engine": "google_scholar"})
14+
client = SerpApiClient({
15+
"q": "Coffee",
16+
"location": "Austin,Texas",
17+
"engine": "google_scholar",
18+
"api_key": os.getenv("API_KEY", "demo")
19+
})
1820
data = client.get_json()
1921
self.assertIsNotNone(data["organic_results"][0]["title"])
2022
pp = pprint.PrettyPrinter(indent=2)

0 commit comments

Comments
 (0)