from urllib.request import urlopen
import json
def downloadPage(url):
webpage = urlopen(url).readlines()
return webpage
json_string = downloadPage('https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=The%20Terminator')
str1 = ''.join(map(bytes.decode, json_string))
parsed_json = json.loads(str1)
print(parsed_json)
似乎json无法正确解析,当我这样做时>>
print(parsed_json['extract'])
我知道
Traceback (most recent call last): File "D:/Universitet/PythonProjects/myapp.py", line 14, in <module> print(parsed_json['extract']) KeyError: 'extract'
我如何使它工作,以便它用我提取想要的json
print(parsed_json['extract'])
从urllib.request导入urlopen导入json def downloadPage(url):网页= urlopen(url).readlines()返回网页json_string = downloadPage('https://en.wikipedia.org/w/api.php?format = ...
如果这是您的JSON对象,那么您必须遍历整个对象以获得要提取的值。
JSON文件具有级别,因此,如果要获取一些值,则应通过键链获取: