Package: scrapy-magicfields
This is a Scrapy spider middleware to add extra fields to items,
based on the configuration settingsMAGIC_FIELDSandMAGIC_FIELDS_OVERRIDE.InstallationInstall scrapy-magicfields usingpip:$ pip install scrapy-magicfieldsConfigurationAdd MagicFieldsMiddleware by including it inSPIDER_MIDDLEWARESin yoursettings.pyfile:SPIDER_MIDDLEWARES = {
    'scrapy_magicfields.MagicFieldsMiddleware': 100,
}Here, priority100is just an example.
Set its value depending on other middlewares you may have enabled already.Enable the middleware usingMAGIC_FIELDS(and optionallyMAGIC_FIELDS_OVERRIDE)
in yoursetting.py.UsageBoth settingsMAGIC_FIELDSandMAGIC_FIELDS_OVERRIDEare dicts:the keys are the destination field names,their value is a string which acceptsmagic variables,
— identified by a starting$(dollar sign),
which will be substituted by a corresponding value at runtime.Some magic variables also accept arguments, and are specified after the magic name,
using a:(column) as separator.You can set project-global magics withMAGIC_FIELDS,
and tune them for a specific spider usingMAGIC_FIELDS_OVERRIDE.In case there is more than one argument, they must come separated by,(comma sign).
So the generic magic format is:$<magic name>[:arg1,arg2,...]Supported magic variables$timethe UTC timestamp at which the item was scraped, in format'%Y-%m-%d%H:%M:%S'.$unixtimethe unixtime (number of seconds since the Epoch, i.e.time.time())
at which the item was scraped.$isotimethe UTC timestamp at which the item was scraped, with format'%Y-%m-%dT%H:%M:%S".$spidermust be followed by an argument,
which is the name of an attribute of the spider (like an argument passed to it).$envthe value of an environment variable.
It acccepts as argument the name of the variable.$jobidthe job id (shortcut for$env:SCRAPY_JOB)$jobtimethe UTC timestamp at which the job started, in format'%Y-%m-%d%H:%M:%S'.$responseAccess to some response properties.$response:urlThe url from where the item was extracted from.$response:statusResponse http status.$response:headersResponse http headers.$settingAccess the given Scrapy setting. It accepts one argument: the name of the setting.$fieldAllows to copy the value of one field to another
Its argument is the source field.
Effects are unpredicable if you use as source a field that is filled
using magic fields.ExamplesThe following configuration will add two fields to each scraped item:'timestamp', which will be filled with the string'item scraped at <scraped timestamp>',and'spider', which will contain the spider nameMAGIC_FIELDS = {
    "timestamp": "item scraped at $time",
    "spider": "$spider:name"
}The following configuration will copy the url to the field sku:MAGIC_FIELDS = {
    "sku": "$field:url"
}Magics also accept a regular expression argument which allows to extract
and assign only part of the value generated by the magic.
You have to specify it using ther''notation.Let’s pretend that the urls of your items look like'http://www.example.com/product.html?item_no=345'and you want to assign to theskufield only the item number.The following example, similar to the previous one but with a second regular expression argument,
will do the task:MAGIC_FIELDS = {
    "sku": "$field:url,r'item_no=(\d+)'"
}