首页 > 解决方案 > 提高 JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1, (char 0), Pushshift

问题描述

我从 None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 得到错误 raise JSONDecodeError("Expecting value", s, err.value)

完整的错误是:Traceback (most recent call last): File "/Users/rukky/Documents/TCD Masters Dissertation/Mining.py", line 60, in <module> data = getPushshiftData(after, before, sub) File "/Users/rukky/Documents/TCD Masters Dissertation/Mining.py", line 18, in getPushshiftData print(r.json()) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/models.py", line 900, in json return complexjson.loads(self.text, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 346, in loads return _default_decoder.decode(s) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我的代码是

def getPushshiftData(after, before, sub):
    #Build URL
    url = 'https://api.pushshift.io/reddit/search/submission/?size=1000&after='+str(after)+'&before='+str(before)+'&subreddit='+str(sub)
    #Print URL to show user
    print(url)
    #Request URL
    r = requests.get(url)
    print(r.status_code)
    print(r.json())
    #Load JSON data from webpage into data variable
    data = json.loads(r.text)
    #return the data element which contains all the submissions data
    return data['data']

第 60 行的第二部分是:

data = getPushshiftData(after, before, sub)
# Will run until all posts have been gathered i.e. When the length of data variable = 0
# from the 'after' date up until before date
while len(data) > 0: #The length of data is the number submissions (data[0], data[1] etc), once it hits zero (after and before vars are the same) end
    for submission in data:
        collectSubData(submission)
        subCount+=1
    # Calls getPushshiftData() with the created date of the last submission
    print(len(data))
    print(str(datetime.datetime.fromtimestamp(data[-1]['created_utc'])))
    #update after variable to last created date of submission
    after = data[-1]['created_utc']
    #data has changed due to the new after variable provided by above code
    data = getPushshiftData( after, before, sub)
    
print(len(data))

我从在线代码中得到了这个,代码本身运行良好。我之前使用过这段代码,它只是偶尔抛出这个错误。我查看了其他问题并尝试调试。整个 JSON 在我的终端中打印出来(意味着它不是空的?),我收到一个 200 代码。我认为问题出在循环中的某个地方。我很感激这方面的任何帮助。

标签: jsonpython-3.xpython-requests

解决方案


推荐阅读