我试图从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": ""
}
]
第一件事是
requests
模块有一个内置的JSON解析方法,所以只需使用它,而不是尝试使用原始文本响应。更改:收件人:
第二,你感兴趣的数据在字典里。但是,该词典包含在一个列表中。获取该列表的第0个索引以访问字典,然后按键(在本例中,也称为“键”)访问该字典。你知道吗
相关问题 更多 >
编程相关推荐