Scrapy - Working with Items
Creating Items
You can create the items as shown in the following format −
>>myproduct = Product(name = 'Mouse', price = 400) >>print myproduct
The above code produces the following result −
Product(name = 'Mouse', price = 400)
Getting Field Values
You can get the field values as shown in the following way −
>>myproduct[name]
It will print result as 'Mouse'
Or in another way, you can get the value using get() method as −
>>myproduct.get(name)
It will print result as 'Mouse'
You can also check whether the field is present or not using the following way −
>>'name' in myproduct
It will print the result as 'True'
Or
>>'fname' in myproduct
It will print the result as 'False'
Setting Field Values
You can set value for the field shown as follows −
>>myproduct['fname'] = 'smith' >>myproduct['fname']
Accessing all Populated Values
It is possible to access all the values, which reside in the 'Product' item.
>>myproduct.keys()
It will print the result as −
['name', 'price']
Or you can access all the values along with the field values shown as follows −
>>myproduct.items()
It will print the result as −
[('name', 'Mouse'), ('price', 400)]
It's possible to copy items from one field object to another field object as described −
>> myresult = Product(myproduct) >> print myresult
It will print the output as −
Product(name = 'Mouse', price = 400)
>> myresult1 = myresult.copy() >> print myresult1
It will print the output as −
Product(name = 'Mouse', price = 400)