1 from scrapy import log 2 from scrapy.contrib.spiders import XMLFeedSpider 3 from myproject.items import TestItem 4 5 class MySpider(XMLFeedSpider): 6 name = 'example.com' 7 allowed_domains = ['example.com'] 8 start_urls = ['http://www.example.com/feed.xml'] 9 iterator = 'iternodes' # This is actually unnecessary, since it's the default value 10 itertag = 'item' #开始进行迭代的节点名称 11 12 def parse_node(self, response, node): 13 log.msg('Hi, this is a <%s> node!: %s' % (self.itertag, ''.join(node.extract()))) 14 15 item = TestItem() 16 item['id'] = node.xpath('@id').extract() 17 item['name'] = node.xpath('name').extract() 18 item['description'] = node.xpath('description').extract() 19 return item
iterator
用于确定使用哪个迭代器的string。可选项有:
默认值为 iternodes
。