我正在尝试从json输出中获取关键代码。但是我似乎不能得到它,我得到左右的错误。这是我的代码。
import requests
import time
import threading
import json
def ThreadRequest():
scrape_url = "https://pastebin.com/api_scraping.php?limit=1"
json_data = requests.get(scrape_url)
python_obj = json.loads(json_data.text)
print python_obj["key"]
ThreadRequest()我要么得到
TypeError: list indices must be integers, not str
ValueError: No JSON object could be decoded
TypeError: expected string or buffer我尝试了许多方法,不同的方法,甚至使用.split()函数进行解析。我似乎无法理解如何在json中进行解析。
以下是API的输出
[
{
"scrape_url": "https://pastebin.com/api_scrape_item.php?i=rkFbtGSj",
"full_url": "https://pastebin.com/rkFbtGSj",
"date": "1516914453",
"key": "rkFbtGSj",
"size": "3031",
"expire": "0",
"title": "",
"syntax": "text",
"user": ""
}
] 发布于 2018-01-26 05:37:51
第一件事是requests模块有一个内置的JSON解析方法,所以只需使用该方法,而不是尝试使用原始文本响应。更改:
python_obj = json.loads(json_data.text)至:
python_obj = json_data.json()其次,您感兴趣的数据在字典中。但是,该字典包含在一个列表中。获取该列表的第0个索引以访问字典,然后通过键(在本例中也称为" key ")进行访问。
my_value = python_obj[0]['key']https://stackoverflow.com/questions/48452206
复制相似问题