You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scrapy存储数据至MongoDB遇TypeError:列表索引需为整数或切片

Fixing the TypeError When Storing Scrapy Data to MongoDB

Hey there! Let's break down why you're hitting this TypeError: list indices must be integers or slices, not str error and fix it step by step.

What's Causing the Problem?

The error message spells it out clearly: you're trying to use a string (like '_id') as an index for a list. In your code, the variable doc is a list, not a dictionary. Lists only accept integer indices (like doc[0]) or slices to access their items—you can't use string keys like you would with a dictionary.

You can even spot an inconsistency in your own code: later on, you correctly use doc[0]['website_url'] (accessing the first item in the doc list, then pulling the website_url key from that dictionary). But earlier, you're trying to access doc['_id'] directly, which is where the error hits.

How to Fix the Code

Assuming doc is a list that contains a single dictionary (which makes sense from your doc[0]['website_url'] line), you just need to adjust how you access the keys in doc—first grab the dictionary from the list with doc[0], then use the string keys.

Here's the corrected version of your code:

alertentry = {
    'website': host,
    '_id': doc[0]['_id'],  # Access the first item in the list first
    'event': 'website_up',
    'alert_id': '',
    'main_website_id': doc[0]['main_website_id']  # Same fix here
}
Alerts.add(alertentry, 'Website Up:' + doc[0]['website_url'], 2)

Quick Tip for Python/Scrapy Newbies

If you're ever confused about what type a variable is, just print it out! Add these lines right before your error-causing code:

print(type(doc))
print(doc)

This will show you if doc is a list, dictionary, or something else, and let you see its exact structure—super helpful for debugging these kinds of index issues.

内容的提问来源于stack exchange,提问作者bhattraideb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:51:34