-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KeyError with the initialization of an Item Field defined with None using ItemLoader #33
Comments
Other workarounds we found could be:
Also, please note that default behavior of |
Hello @NadirRisc and @vincent-ferotin. In my opinion it's OK for the code that uses an It's also fine to subclass item and item loader classes to get the behavior you need. Also, you can subclass an exporter and use Finally, I don't think it's OK to break the existing behavior by keeping empty values as the common pattern with item loaders assumes you don't know if the value is empty at the run time and want to ignore those (this is mostly about |
Newbie here, I'm having the same problem and a hard time understanding the workaround. The issue is that when the scraper encounters an empty field, it raises a keyerror and that item gets dropped. I want to be able to store the data, even if some of the fields are empty. Would any of you be able to better explain and walk me through the workaround? Here's an example of the error:
Here is what I assume is the relevant source code:
I also have a pipeline that loads everything into my DB. |
I used Nadir's solution and it works perfect for my purposes. I tweaked it a bit to catch any that are not set and added a "Not set" default.
Thanks Nadir! :) |
what is the exact function about the code?
so, if the value of field is None, then item loader won't insert the key-value to the fields, and it will raise the KeyError if you want to get the key in the item. |
I think we can work something here. Maybe we can create some kind of setting in the Also, I think this one is related scrapy/scrapy#2498 |
Thanks @dashw00d and @NadirRisc, perfect solutions!!!! |
Using Scrapy 1.5.0
I took a look at the FAQ section and nothing was relevant about it.
Same for issues with keyword
KeyError
on github, Reddit, or GoogleGroups.As you can see below, it seems to me that here is an inconsistency when we load an
Item
or initialize it with a values asNone
or an empty string. First we add a value to our field (here title) through aItemLoader
. Then the loader creates an item with theload_item()
method. Once it's done we can't access the field if the value wasNone
or an empty string. The inconsistency is that the other method (initializing anItem
directly with a field set toNone
or empty string) doesn't raise aKeyError
(field is set).The class
TakeFirst
don't return any value when they're set withNone
or an empty string. Which prevents the methodload_item()
inItemLoader
class to add an entry to the field.Here is a minimal source code that represents the inconsistency.
We're using
Python 3.5
for our project and found the following workaround to prevent this error.We introduce a new class (
DefaultAwareItem
) which fulfills unset fields weredefault metadata
has been set previously.The text was updated successfully, but these errors were encountered: