浏览代码

wikipedia

master
Victor Fors 2 年前
父节点
当前提交
7af284a92f
共有 1 个文件被更改,包括 7 次插入3 次删除
  1. +7
    -3
      wikipedia

+ 7
- 3
wikipedia 查看文件

@@ -2,24 +2,28 @@
import requests
import sys
import textwrap
try:
from simplejson.errors import JSONDecodeError
except ImportError:
from json.decoder import JSONDecodeError

# Fetch JSON from url and run it through transform, pretty printing errors
# and the data worked on as exhaustively as possible.
def json_query(url, transform, params={}):
try:
result = requests.get(url, params)
except ConnectionError:
except requests.exceptions.ConnectionError:
print("Network connection error.")
sys.exit(1)
try:
data = result.json()
except JSONDecodeError as err:
print('Error when decoding JSON:\nFrom endpoint ' + url + ':\n' + err + '\n' + result + '\n')
print('Error when decoding JSON:\nFrom endpoint ' + url + ':\n' + str(err) + '\n' + str(result) + '\n')
sys.exit(1)
try:
return transform(data)
except (IndexError, KeyError) as err:
print('Error when traversing JSON:\nFrom endpoint ' + url + ':\n' + err)
print('Error when traversing JSON:\nFrom endpoint ' + url + ':\n' + str(err))
pprint.PrettyPrinter(indent=2).pprint(data)
sys.exit(1)



正在加载...
取消
保存