XMLFeedSpider例子

 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值