2016-12-05 250 views
0

所以,我的代码如下所示:类型错误:列表索引必须是整数或片,而不是str的<编码错误>

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 

而且,当我运行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

可能是什么造成的呢?

顺便说,数据我取这个样子的:

[ 
    { 
    "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

由于您使用的是串试图索引列表。看看数据,它是一个数组内的单个对象。 –

+0

建议像'ret = req.json()一样提取属性。 ID = RET [ “ID”] [0]',然后通过他们构造器'邮报(ID,...' – georgexsh

+0

@georgexsh它仍然以这种方式返回相同的错误... –

回答

0

我发现这个问题其实。而不是req.json()["id"],它应该是req.json()[0]["id"]

相关问题