Scrapy未从json中识别关键字

jtjikinw  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(720)

我试图从一个网站上抓取有关圣经注解的信息。下面是我为此所做的代码。start_urls是我试图抓取的json文件的链接。我选择了['0']['father']['_id']来获取评论者的名称,但是,出现了下面的错误。我该怎么办?
错误:TypeError: list indices must be integers or slices, not str
编码:

import scrapy
import json

class catenaspider(scrapy.Spider): #spider to crawl the url
    name = 'commentary' #name to be called in command terminal
    start_urls = ['https://api.catenabible.com:8080/anc_com/c/mt/1/1?tags=[%22ALL%22]&sort=def']

    def parse(self,response):
        data = json.loads(response.body)
        yield from data['0']['father']['_id']```
osh3o9ms

osh3o9ms1#

请再次阅读文档。

import scrapy

class catenaspider(scrapy.Spider):  # spider to crawl the url
    name = 'commentary' # name to be called in command terminal
    start_urls = ['https://api.catenabible.com:8080/anc_com/c/mt/1/1?tags=[%22ALL%22]&sort=def']

    def parse(self, response):
        data = response.json()
        yield {'id_father': data[0]['father']['_id']}
        # if you want to get all the id's
        # for d in data:
        #     yield {'id_father': d['father']['_id']}

相关问题