简单的废XML spider语法

2024-06-16 10:08:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我只是想做一个简单的spider,使用scrapy从XML文件中获取数据。这就是我想到的:

from scrapy.contrib.spiders import XMLFeedSpider

class MySpider(XMLFeedSpider):
     name = 'testproject'
     allowed_domains = ['www.w3schools.com']
     start_urls = ['http://www.w3schools.com/xml/note.xml']
     itertag = 'note'

    def parse_node(self, response, node):
         to = node.select('to/text()').extract()
         from = node.select('from/text()').extract()
         heading  = node.select('heading/text()').extract()
         body  = node.select('body/text()').extract()

    def parse_node(self, response, node):
         log.msg('Hi, this is a <%s> node!: %s' % (self.itertag, ''.join(node.extract())))

        item = Item()
         item['to'] = node.select('to').extract()
         item['from'] = node.select('from').extract()
         item['heading'] = node.select('heading').extract()
         item['body'] = node.select('body').extract()
         return item

这是数据集: http://www.w3schools.com/xml/note.xml

当我尝试运行它时,不幸的是它不起作用。我想这和我如何映射标签有关。这是错误:

^{pr2}$

任何帮助都将不胜感激。在


Tags: totextfromselfcomnodewwwextract