Scrapy: Define items dynamically

前端 未结 5 1379
别跟我提以往
别跟我提以往 2021-02-05 10:23

As I started to learn scrapy, i have come accross a requirement to dynamically build the Item attributes. I\'m just scraping a webpage which has a table structure and I wanted t

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-05 10:50

    I was more xpecting about explanation in handling the data with item loaders and pipelines

    Assuming:

    fieldname = 'test'
    fieldxpath = '//h1'
    

    It's (in recent versions) very simple...

    item = Item()
    l = ItemLoader(item=item, response=response)
    
    item.fields[fieldname] = Field()
    l.add_xpath(fieldname, fieldxpath)
    
    return l.load_item()
    

提交回复
热议问题