Hi,
regarding question 1.,
you can create your own XML feed exporter class,
subclassing the default XmlItemExporter class
calling XmlItemExporter's __init__ method with 2 arguments root_element and
item_element, to override the default "items" and "item" node names,
and overriding the method writing node values and multi-valued nodes
from scrapy.contrib.exporter import XmlItemExporter
class CustomXmlItemExporter(XmlItemExporter):
def __init__(self, file, **kwargs):
super(CustomXmlItemExporter, self).__init__(file,
root_element="ads",
item_element="ad",
**kwargs)
def _export_xml_field(self, name, serialized_value):
self.xg.startElement(name, {})
if hasattr(serialized_value, 'items'):
for subname, value in serialized_value.items():
self._export_xml_field(subname, value)
elif hasattr(serialized_value, '__iter__'):
for value in serialized_value:
self._export_xml_field('customnodename', value)
else:
self._xg_characters(serialized_value)
self.xg.endElement(name)
Finally, you should tell scrapy to use this custom exporter via
settings.py, something like this:
FEED_EXPORTERS = {
'xml': 'myproject.exporter.CustomXmlItemExporter',}
Hope this helps.
Paul.
On Monday, May 26, 2014 10:08:03 AM UTC+2, [email protected] wrote:
>
> Hello!
>
> Could someone get time to look into this and providing some solution?
>
> Thanks,
>
> On Wednesday, May 21, 2014 4:15:26 PM UTC+5:30, [email protected] wrote:
>>
>> Hi,
>>
>> I am beginners with scrapy and also with the python. So far I could
>> manage to create some basic spiders based on given examples in scrapy doc.
>>
>> Now I would like to extend the default features according to custom
>> requirement. Atm I am trying to change the default tag names in output xml.
>> For example,
>>
>> 1) By default it generates xml with item_element='item' and
>> root_element='items', instead I want to change it to item_element='ad' and
>> root_element='ads'. Also multi-value fields are exported by each value
>> inside the <value> element which i want to change with another name.
>>
>> 2) I want to split the data into multiple xmls based on the number of
>> records per xml. For example, 5000 <ad> per xml.
>>
>> I have tried to look into
>> http://doc.scrapy.org/en/0.22/topics/exporters.html#xmlitemexporter but
>> I could not get clear idea. Could someone provide me exact filename and
>> code which can be used to implement this change?
>>
>> Any help / suggestions would be appreciated.
>>
>> Thanks,
>>
>>
>>
--
You received this message because you are subscribed to the Google Groups
"scrapy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/scrapy-users.
For more options, visit https://groups.google.com/d/optout.