-
Notifications
You must be signed in to change notification settings - Fork 10.6k
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
Feature Request: Item class field access and item creation using this #2749
Comments
Hi @Manslow , |
Hi, I am looking into this, and while it can be done, I was wondering whether a simpler solution would achieve the same aims. I am not familiar with the
mentioned by @Manslow.
Please, let me know if this would work for your aims. |
I'm not opposed to adding https://github.com/python-attrs/attrs support to Scrapy, so that objects created with A simplest implementatin could be the following - check if an object has .todict() method; call it, if it is present; use the reulting dict as a Scrapy item. Maybe we can also provide a scrapy.BetterItem based on attrs; missing parts are |
I attempted an implementation in the PR above. With that implementation, certain arguments need to be passed onto the @attr.s decorator. I couldn't find a way to avoid that but would be happy to know if there is one, as that seems inconvenient, and something left to the proper usage of Scrapy Items with the decorator. |
I believe #3881 has implemented this by supporting dataclasses and attr.s as items. |
I agree with @Manslow. facing with 200+ field and more than 150 sites use same item class. |
Items provide a simple way to structure data. However, as items are currently implemented, you can only populate an item by referencing a field using a string that matches the field's name e.g
item['field_name'] = value
. This becomes a problem when you want to change the name of a field after creation. It also requires that you remember the name of the fields defined for an item.I think it would be nice to be able to populate items using syntax like the following:
In this way, you can use code completion in an IDE to be sure that you are referencing a field the Item class has defined and can leverage refactoring to change the names of fields across your entire project wherever Item.field is referenced.
Perhaps there is a good reason why this option was discarded during development but it isn't obvious to me why.
The text was updated successfully, but these errors were encountered: