TypeError:list indices必须是整数或切片,而不是str

时间:2022-08-14 19:36:18

So, my code looks like this:

所以,我的代码看起来像这样:

import requests
import random


def load():
    req = requests.get(https: // yande.re / post.json?tags = rating % 3
    Asafe + -pantyshot + -panties + & ms = 1 & page = 12650 & limit = 1)

    data = Posts(req.json()["id"][0], req.json()["tags"], slice(req.json()["creator_id"]), req.json()["author"],
                 req.json()["source"],
                 req.json()["score"], req.json()["md5"], req.json()["file_url"], req.json()["sample_url"],
                 req.json()["width"],
                 req.json()["height"])

    all = data.tags, data.creator_id, data.author, data.source, data.score, data.md5, data.file_url, data.sample_url, data.width, data.height

    return all

And, when I run the load(), I have this output:

而且,当我运行load()时,我有这个输出:

Traceback (most recent call last): File "", line 134, in File "", line 126, in anime TypeError: list indices must be integers or slices, not str

回溯(最近一次调用最后一次):文件“”,第134行,在文件“”,第126行,动画中TypeError:列表索引必须是整数或切片,而不是str

What could be causing it?

可能是什么原因造成的?

By the way, the data I'm fetching looks like this:

顺便说一下,我提取的数据看起来像这样:

[
  {
    "actual_preview_height": 218,
    "jpeg_url": "https://files.yande.re/image/32a001e7b5050828c9b07e62de634958/yande.re%20376617%20dress%20novelance%20see_through.jpg",
    "status": "active",
    "preview_url": "https://assets.yande.re/data/preview/32/a0/32a001e7b5050828c9b07e62de634958.jpg",
    "has_children": false,
    "source": "http://i2.pixiv.net/img-original/img/2016/12/05/00/00/10/60241721_p0.jpg",
    "score": 1,
    "height": 1392,
    "rating": "s",
    "id": 376617,
    "last_commented_at": 0,
    "frames": [],
    "md5": "32a001e7b5050828c9b07e62de634958",
    "updated_at": 1480900734,
    "creator_id": 280440,
    "frames_pending_string": "",
    "frames_string": "",
    "actual_preview_width": 300,
    "is_shown_in_index": true,
    "frames_pending": [],
    "change": 1992459,
    "last_noted_at": 0,
    "approver_id": null,
    "is_held": false,
    "preview_width": 150,
    "tags": "dress novelance see_through",
    "preview_height": 109,
    "created_at": 1480900721,
    "file_ext": "jpg",
    "sample_height": 1088,
    "sample_url": "https://files.yande.re/sample/32a001e7b5050828c9b07e62de634958/yande.re%20376617%20sample%20dress%20novelance%20see_through.jpg",
    "parent_id": null,
    "width": 1920,
    "jpeg_file_size": 0,
    "sample_file_size": 478570,
    "author": "LolitaJoy",
    "file_size": 989513,
    "file_url": "https://files.yande.re/image/32a001e7b5050828c9b07e62de634958/yande.re%20376617%20dress%20novelance%20see_through.jpg",
    "is_note_locked": false,
    "is_pending": false,
    "sample_width": 1500,
    "jpeg_width": 1920,
    "jpeg_height": 1392,
    "is_rating_locked": false
  }
]

1 个解决方案

#1


0  

I found the problem actually. instead of req.json()["id"], it should have been req.json()[0]["id"]

我实际上发现了这个问题。而不是req.json()[“id”],它应该是req.json()[0] [“id”]

#1


0  

I found the problem actually. instead of req.json()["id"], it should have been req.json()[0]["id"]

我实际上发现了这个问题。而不是req.json()[“id”],它应该是req.json()[0] [“id”]