Python 零散物品的键中具有非常规字符的自定义字段
假设我想定义一个名为Product的项模型,它有一个名为@type的键 显然,以下定义在python中是非法的,因为@type不是有效的实例变量名 仍然可以使用如下JSON:Python 零散物品的键中具有非常规字符的自定义字段,python,scrapy,Python,Scrapy,假设我想定义一个名为Product的项模型,它有一个名为@type的键 显然,以下定义在python中是非法的,因为@type不是有效的实例变量名 仍然可以使用如下JSON: { name: "Battery", price: 1.00, stock: 10, @type: "Product" } 有人知道如何在Scrapy中正确执行此操作吗?因为它基于dict并将字段存储在,override\uuu init\uuuuu: 蜘蛛网示例: from scrapy import
{
name: "Battery",
price: 1.00,
stock: 10,
@type: "Product"
}
有人知道如何在Scrapy中正确执行此操作吗?因为它基于dict并将字段存储在,override\uuu init\uuuuu:
蜘蛛网示例:
from scrapy import Item, Field
from scrapy import Spider
class Product(Item):
name = Field()
price = Field()
stock = Field()
def __init__(self, *args, **kwargs):
super(Product, self).__init__(*args, **kwargs)
self.fields['@type'] = Field()
class ProductSpider(Spider):
name = "product_spider"
start_urls = ['http://google.com']
def parse(self, response):
item = Product()
item['name'] = 'Test name'
item['price'] = 0
item['stock'] = True
item['@type'] = 'Test type'
return item
产生:
$ scrapy runspider spider1.py
2014-08-11 14:32:00-0400 [product_spider] DEBUG: Scraped from <200 http://www.google.com/>
{'@type': 'Test type', 'name': 'Test name', 'price': 0, 'stock': True}
from scrapy import Item, Field
from scrapy import Spider
class Product(Item):
name = Field()
price = Field()
stock = Field()
def __init__(self, *args, **kwargs):
super(Product, self).__init__(*args, **kwargs)
self.fields['@type'] = Field()
class ProductSpider(Spider):
name = "product_spider"
start_urls = ['http://google.com']
def parse(self, response):
item = Product()
item['name'] = 'Test name'
item['price'] = 0
item['stock'] = True
item['@type'] = 'Test type'
return item
$ scrapy runspider spider1.py
2014-08-11 14:32:00-0400 [product_spider] DEBUG: Scraped from <200 http://www.google.com/>
{'@type': 'Test type', 'name': 'Test name', 'price': 0, 'stock': True}