从列表中向scrapy中的项目添加字段

时间:2018-07-01 10:50:28

标签: scrapy field

我有一长串要添加到刮板物品的字段,但不知道该怎么做。基本上我正在尝试获取

的输出
class myItem(scrapy.Item):
    a = scrapy.field()
    b = scrapy.field()

但是我想从

之类的列表中创建它
['a','b']

我尝试过

fields = ['a','b']

def generateFields(fields):  
    item = myItem()
    for field in fields:
        field = scrapy.Field()
    return item

但是那没有用。谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用python魔术方法__dict__来管理它,几乎是这样的:

fields = ['a', 'b', 'c']
class MyItem(scrapy.Item):
    def __init__(self):
        for f in fields:
            self.__dict__[f] = scrapy.field()

my_item = MyItem()

以此类推... 看来我先前的评论对普通的python类有效,而不对scrapy.Item类有效。以下代码正在工作:

from scrapy import Item, Field

fields = ['a', 'b', 'c']

def generate_item(fields):
    item = Item()
    for f in fields:
        item.fields[f] = Field()
    return item

 item_instance = generate_item(fields)
 item_instance['a'] = 'some_value'
 item_instance['b'] = 12
 print(item_instance)