Scrapy存储数据至MongoDB遇TypeError:列表索引需为整数或切片
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

