Skip to content

Commit caa9fbb

Browse files
committed
2021-09-01 @ 2.4.1
- raise SerpApiClientException instead of raw string in order to follow Python guideline 3.5+ - add more unit error tests for serp_api_clien
1 parent 10b8dd0 commit caa9fbb

24 files changed

+117
-47
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ install:
1919
test:
2020
pytest
2121

22-
# run example only
22+
# run example only
23+
# and display output (-s)
2324
example:
2425
pytest -s "tests/test_example.py::TestExample::test_async"
2526

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ See the [playground to generate your code.](https://serpapi.com/playground)
8484
- [Batch Asynchronous Searches](#batch-asynchronous-searches)
8585
- [Python object as a result](#python-object-as-a-result)
8686
- [Python paginate using iterator](#pagination-using-iterator)
87+
- [Error management](#error-management)
8788
- [Change log](#change-log)
8889
- [Conclusion](#conclusion)
8990

@@ -191,6 +192,7 @@ The previous search can be retrieve from the the cache for free.
191192
from serpapi import GoogleSearch
192193
search = GoogleSearch({"q": "Coffee", "location": "Austin,Texas"})
193194
search_result = search.get_dictionary()
195+
assert search_result.get("error") == None
194196
search_id = search_result.get("search_metadata").get("id")
195197
print(search_id)
196198
```
@@ -526,7 +528,27 @@ if len(urls) == len(set(urls)):
526528

527529
Examples to fetch links with pagination: [test file](https://github.com/serpapi/google-search-results-python/blob/master/tests/test_example_paginate.py), [online IDE](https://replit.com/@DimitryZub1/Scrape-Google-News-with-Pagination-python-serpapi)
528530

531+
### Error management
532+
533+
SerpAPI keeps error mangement very basic.
534+
- backend service error or search fail
535+
- client error
536+
537+
If it's a backend error, a simple message error is returned as string in the server response.
538+
```python
539+
from serpapi import GoogleSearch
540+
search = GoogleSearch({"q": "Coffee", "location": "Austin,Texas", "api_key": "<secret_key>"})
541+
data = search.get_json()
542+
assert data["error"] == None
543+
```
544+
In some case, there is more details availabel in the data object.
545+
546+
If it's client error, then a SerpApiClientException is raised.
547+
529548
## Change log
549+
2021-09-01 @ 2.4.1
550+
- raise SerpApiClientException instead of raw string in order to follow Python guideline 3.5+
551+
- add more unit error tests for serp_api_client
530552
2021-07-26 @ 2.4.0
531553
- add page size support using num parameter
532554
- add youtube search engine

serpapi/baidu_search.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from serpapi.serp_api_client import *
2+
from serpapi.serp_api_client_exception import SerpApiClientException
23

34
class BaiduSearch(SerpApiClient):
45
"""BaiduSearch enables to search baidu and parse the result.
@@ -15,4 +16,4 @@ def __init__(self, params_dict):
1516
super(BaiduSearch, self).__init__(params_dict, BAIDU_ENGINE)
1617

1718
def get_location(self, q, limit = 5):
18-
raise "location is not supported by Baidu search engine at this time"
19+
raise SerpApiClientException("location is not supported by Baidu search engine at this time")

serpapi/ebay_search.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from serpapi.serp_api_client import *
2+
from serpapi.serp_api_client_exception import SerpApiClientException
23

34
class EbaySearch(SerpApiClient):
45
"""EbaySearch enables to search ebay and parse the result.
@@ -15,4 +16,4 @@ def __init__(self, params_dict):
1516
super(EbaySearch, self).__init__(params_dict, EBAY_ENGINE)
1617

1718
def get_location(self, q, limit = 5):
18-
raise "location is not supported by Ebay search engine at this time"
19+
raise SerpApiClientException("location is not supported by Ebay search engine at this time")

serpapi/google_scholar_search.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from serpapi.serp_api_client import *
2+
from serpapi.serp_api_client_exception import SerpApiClientException
23

34
class GoogleScholarSearch(SerpApiClient):
45
"""GoogleScholarSearch enables to search google scholar and parse the result.
@@ -15,4 +16,4 @@ def __init__(self, params_dict):
1516
super(GoogleScholarSearch, self).__init__(params_dict, GOOGLE_SCHOLAR_ENGINE)
1617

1718
def get_location(self, q, limit = 5):
18-
raise "location is not supported by Google scholar search engine"
19+
raise SerpApiClientException("location is not supported by Google scholar search engine")

serpapi/home_depot_search.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from serpapi.serp_api_client import *
2+
from serpapi.serp_api_client_exception import SerpApiClientException
23

34
class HomeDepotSearch(SerpApiClient):
45
"""HomeDepotSearch enables to search home depot and parse the result.
@@ -15,4 +16,4 @@ def __init__(self, params_dict):
1516
super(HomeDepotSearch, self).__init__(params_dict, HOME_DEPOT_ENGINE)
1617

1718
def get_location(self, q, limit = 5):
18-
raise "location is not supported by Home Depot search engine"
19+
raise SerpApiClientException("location is not supported by Home Depot search engine")

serpapi/pagination.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pprint
1+
from serpapi.serp_api_client_exception import SerpApiClientException
22

33
DEFAULT_START = 0
44
DEFAULT_END = 1000000000
@@ -28,9 +28,9 @@ def __init__(self, client, start = DEFAULT_START, end = DEFAULT_END, num = DEFAU
2828

2929
# basic check
3030
if self.start > self.end:
31-
raise "start: {} must be less than end: {}".format(self.start, self.end)
31+
raise SerpApiClientException("start: {} must be less than end: {}".format(self.start, self.end))
3232
if(self.start + self.num) > self.end:
33-
raise "start + num: {} + {} must be less than end: {}".format(self.start, self.num, self.end)
33+
raise SerpApiClientException("start + num: {} + {} must be less than end: {}".format(self.start, self.num, self.end))
3434

3535
def __iter__(self):
3636
self.update()

serpapi/serp_api_client.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import requests
22
import json
33
from serpapi.pagination import Pagination
4+
from serpapi.serp_api_client_exception import SerpApiClientException
45

56
GOOGLE_ENGINE = 'google'
67
BING_ENGINE = 'bing'
@@ -28,7 +29,8 @@ class SerpApiClient(object):
2829
https://serpapi.com/search-api
2930
"""
3031

31-
BACKEND = "https://serpapi.com"
32+
#BACKEND = "https://serpapi.com"
33+
BACKEND = "http://localhost:3000"
3234
SERP_API_KEY = None
3335

3436
def __init__(self, params_dict, engine = None, timeout = 60000):
@@ -44,8 +46,7 @@ def construct_url(self, path = "/search"):
4446
if not 'engine' in self.params_dict:
4547
self.params_dict['engine'] = self.engine
4648
if not 'engine' in self.params_dict:
47-
raise "engine must be defined in params_dict or engine"
48-
49+
raise SerpApiClientException("engine must be defined in params_dict or engine")
4950
return self.BACKEND + path, self.params_dict
5051

5152
def get_response(self, path = '/search'):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class SerpApiClientException(Exception):
2+
pass

serpapi/yahoo_search.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from serpapi.serp_api_client import *
2+
from serpapi.serp_api_client_exception import SerpApiClientException
23

34
class YahooSearch(SerpApiClient):
45
"""YahooSearch enables to search yahoo and parse the result.
@@ -15,4 +16,4 @@ def __init__(self, params_dict):
1516
super(YahooSearch, self).__init__(params_dict, YAHOO_ENGINE)
1617

1718
def get_location(self, q, limit = 5):
18-
raise "location is not supported by Yahoo search engine at this time"
19+
raise SerpApiClientException("location is not supported by Yahoo search engine at this time")

0 commit comments

Comments
 (0)