网络抓取时出现JSONDecodeError

时间:2020-09-19 13:57:52

标签: python json web-scraping request

我正在尝试使用request从网站列表中获取年龄,如下所示:

import json
import requests


my_list=['www.expedia.it',  'www.fortuneita.com',   'www.finanzaonline.com',    'it-it.facebook.com',   'www.capterra.it',  'www.sportmediaset.mediaset.it',    'www.incredibile.net',  'www.newsrimini.it',    'web.tiscali.it',   'www.dettiescritti.com']

api_url = 'https://web.archive.org/__wb/search/metadata'

for url in my_list:
        data = requests.get(api_url, params={'q': url}).json()     
        min_year = min(data['urls'], key=int)

但是我遇到了这个错误:

JSONDecodeError                           Traceback (most recent call last)
<ipython-input-27-e091c19d8374> in <module>

     15     for url in my_list:
---> 16         data = requests.get(api_url, params={'q': url}).json()


/anaconda3/lib/python3.7/site-packages/requests/models.py in json(self, **kwargs)
    896                     # used.
    897                     pass
--> 898         return complexjson.loads(self.text, **kwargs)
    899 
    900     @property

/anaconda3/lib/python3.7/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

/anaconda3/lib/python3.7/json/decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

/anaconda3/lib/python3.7/json/decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我查看了以下问题:JSONDecodeError: Expecting value: line 1 column 1 (char 0) 但是我没有解决这个问题的方法。

您知道我该如何解决吗?几分钟前一切正常,我没有做任何更改。

1 个答案:

答案 0 :(得分:0)

当由于某些原因出现网络错误或API调用失败时,会发生这种情况。 我亲自尝试了您的代码,并且效果很好。在这种情况下,您可以try-except保护您的代码。

示例:

for url in my_list:
        data = None
        res = requests.get(api_url, params={'q': url})
        try:
            data = res.json()
        except:
            data = {"url" : "SOME_DEFAULT_VALUE"}
        min_year = min(data['urls'], key=int)

Screenshot