简体   繁体   English

TypeError:列表索引必须是整数或切片,而不是str <encoding error>

[英]TypeError: list indices must be integers or slices, not str <encoding error>

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 追溯(最近一次呼叫最近):动漫TypeError中的文件“”的第134行,动画的文件“”的第126行:列表索引必须是整数或切片,而不是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
  }
]

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"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “TypeError:list indices必须是整数或切片,而不是str” - “TypeError: list indices must be integers or slices, not str” TypeError:列表索引必须是整数或切片,而不是 str - TypeError: List indices must be integers or slices and not str 类型错误:列表索引必须是整数或切片,而不是 str - TypeError: list indices must be integers or slices, not str “TypeError:列表索引必须是整数或切片,而不是 str” - "TypeError: list indices must be integers or slices, not str" TypeError:列表索引必须是整数或切片而不是 str - TypeError: list indices must be integers or slices not str Python 错误:类型错误:列表索引必须是整数或切片,而不是 str - Python Error: TypeError: list indices must be integers or slices, not str 错误列表索引必须是整数或切片,而不是str - error list indices must be integers or slices, not str Python3-TypeError:列表索引必须是整数或切片,而不是str-List - Python3 - TypeError: list indices must be integers or slices, not str - List 使用 Python TypeError 解析 JSON:列表索引必须是整数或切片,而不是 str - Parsing JSON with Python TypeError: list indices must be integers or slices, not str 如何解决“类型错误:列表索引必须是整数或切片,而不是 str” - How to solve "TypeError: list indices must be integers or slices, not str"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM