XMLFeedSpider例子

mac2022-06-30  16

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' - 一个高性能的基于正则表达式的迭代器'html' - 使用 Selector 的迭代器。 需要注意的是该迭代器使用DOM进行分析,其需要将所有的DOM载入内存, 当数据量大的时候会产生问题。'xml' - 使用 Selector 的迭代器。 需要注意的是该迭代器使用DOM进行分析,其需要将所有的DOM载入内存, 当数据量大的时候会产生问题。

默认值为 iternodes 。

转载于:https://www.cnblogs.com/programerlrc/p/4657192.html

最新回复(0)