diff --git a/src/scrapy_redis/spiders.py b/src/scrapy_redis/spiders.py index c82ad2e3..351c5ba5 100644 --- a/src/scrapy_redis/spiders.py +++ b/src/scrapy_redis/spiders.py @@ -7,7 +7,7 @@ import time from . import connection, defaults -from .utils import bytes_to_str +from .utils import bytes_to_str, is_dict class RedisMixin(object): @@ -171,7 +171,7 @@ def make_request_from_data(self, data): # change to json array parameter = {} - if type(formatted_data) == dict: + if is_dict(formatted_data): parameter = json.loads(formatted_data) else: print(TextColor.WARNING + "WARNING: String request is deprecated, please use JSON data format. \ diff --git a/src/scrapy_redis/utils.py b/src/scrapy_redis/utils.py index fcaa649b..14674cce 100644 --- a/src/scrapy_redis/utils.py +++ b/src/scrapy_redis/utils.py @@ -1,3 +1,6 @@ +import json +from json import JSONDecodeError + import six @@ -11,9 +14,16 @@ class TextColor: ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' - + def bytes_to_str(s, encoding='utf-8'): """Returns a str if a bytes object is given.""" if six.PY3 and isinstance(s, bytes): return s.decode(encoding) return s + +def is_dict(string_content): + try: + json.loads(string_content) + except JSONDecodeError: + return False + return True